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.
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.