Filling Data to last row or setting value of cells to last row in office script

Youngdand

Board Regular
Joined
Sep 29, 2017
Messages
123
Hi,

I have an office script that will be run though Power Automate. the automation extracts an email attachment, saves the file, then runs the office script, which deletes unnecessary images, header information and columns, and then needs to remove the last entire row, then change the values in the 4th column (D) to PT0006.

I have managed to perform all most of the steps, but am unable to get the last row and delete, and change the values in column 4 to paste, or set value to the last row of the sheet .

this is the script i have used:

function main(workbook: ExcelScript.Workbook) {
let selectedSheet = workbook.getActiveWorksheet();
let usedRange = selectedSheet.getUsedRange();
let lastRowAddress = usedRange.getLastRow().getRowIndex();
// Delete shapes
let sheets = workbook.getWorksheets();
for (let sheet of sheets) {
sheet.getShapes().forEach((shape, index) => {
if (shape.getType() === ExcelScript.ShapeType.image) {
shape.delete();
}
});
}
// Delete range 1:9 on selectedSheet
selectedSheet.getRange("1:9").delete(ExcelScript.DeleteShiftDirection.up);
// Delete range A:Fnon selectedSheet
selectedSheet.getRange("A:F").delete(ExcelScript.DeleteShiftDirection.left);
// Change Column titles
selectedSheet.getRange("A1:D1").setValues([["VRM", "PermitStartDate", "PermitEndDate", "PermitTypeRef"]]);
//selectedSheet.getRange("D2:D & (LastRowAddress)").setValue("PT0006");
selectedSheet.getRange("D2").setValue("PT0006");
selectedSheet.getRange("D2").autoFill("D2:D & (lastRowAddress)", ExcelScript.AutoFillType.fillCopy);
// Set range A1:D1 on sheet2
selectedSheet.getRange().getFormat().autofitColumns();
}


Thanks for any help you can give me.

Cheers,

Dan.
 

Attachments

  • Original File.PNG
    Original File.PNG
    48.4 KB · Views: 37
  • Output.PNG
    Output.PNG
    13.5 KB · Views: 38

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Would you mind posting your solution?
If you do that, you can then also mark that post as the solution.
 
Upvote 0
JavaScript:
function main(workbook: ExcelScript.Workbook) {
    let selectedSheet = workbook.getActiveWorksheet();
    let usedRange = selectedSheet.getUsedRange();
    let lastRowAddress = usedRange.getLastRow().getRowIndex();


    // Delete shape picture_1
    let sheets = workbook.getWorksheets();
    for (let sheet of sheets) {
        sheet.getShapes().forEach((shape, index) => {
            if (shape.getType() === ExcelScript.ShapeType.image) {
                shape.delete();
            }
        });
    }
    //Clear Formatting
    usedRange.clear(ExcelScript.ClearApplyTo.formats)
    //Delete Total Row
    selectedSheet.getRange(("A" + lastRowAddress) + ":J" + lastRowAddress).delete(ExcelScript.DeleteShiftDirection.up);
    // Delete range 1:9 on selectedSheet
    selectedSheet.getRange("1:9").delete(ExcelScript.DeleteShiftDirection.up);
    //Set Date Format
    selectedSheet.getRange("H:I").setNumberFormat("DD/MM/YYYY HH:MM:SS")
    //Delete Unnecessary Columns
    selectedSheet.getRange("A:F").delete(ExcelScript.DeleteShiftDirection.left);
    //Rename Columns
    selectedSheet.getRange("A1:D1").setValues([["VRM", "PermitStartDate", "PermitEndDate", "PermitTypeRef"]]);
  
    // Update the usedRange after deletion and get new last row adress
    usedRange = selectedSheet.getUsedRange();
    lastRowAddress = usedRange.getLastRow().getRowIndex();
    lastRowAddress = lastRowAddress+1
    // Add PetmitTypeRef to each Row
    selectedSheet.getRange("D2:D" + lastRowAddress).setValue("6");
  
}
 
Last edited by a moderator:
Upvote 0
Solution

Forum statistics

Threads
1,223,881
Messages
6,175,161
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top