Use a script to copy, cut, paste, replace and delete in an excel report (part 2)
This is the second part of my tutorial and it will be focused on copying, cutting, pasting, replacing, deleting and inserting. If you missed the first part, read Use a script to create, edit and select in an excel report (part 1).
To copy and/or to cut:
function main(workbook: ExcelScript.Workbook) { //copy cell A2 then paste it in D5 workbook.getWorksheet('Sheet2').getRange("D5").copyFrom("A2"); //cut cell A2:B10 then paste it in A2 of sheet3 workbook.getWorksheet('Sheet2').getRange("A2:B10").moveTo(workbook.getWorksheet("Sheet3") .getRange("A2")); //copy cell A2 of sheet1 then paste it in A2 of sheet3 workbook.getWorksheet('Sheet3').getRange("A2").copyFrom(workbook.getWorksheet("Sheet1") .getRange("A2")); }
This is a simple paste, if I want to paste as values:
function main(workbook: ExcelScript.Workbook) { //copy cell A2 then paste as values in D5 workbook.getWorksheet('Sheet2').getRange("D5").copyFrom("A2", ExcelScript.RangeCopyType.values); }
To put a formula:
function main(workbook: ExcelScript.Workbook) { //put formula in cell D2 workbook.getWorksheet('Sheet2').getRange("D2").setFormula("=VLOOKUP(\"january\",A2:B13,2,0)") }
This code will scroll/drag down the selected cell by copying it to a specific number of rows by pasting it:
function main(workbook: ExcelScript.Workbook) { //copy cell A2 then paste it until row A25 //for more columns, change A13 by A13:D13 and A13:A18 by A13:D18 workbook.getWorksheet('Sheet2').getRange("A13").autoFill("A13:A18", ExcelScript.AutoFillType.fillDefault); }
To delete the content of a cell:
function main(workbook: ExcelScript.Workbook) { //clear all values in cells B2:B5 workbook.getWorksheet('Sheet2').getRange("B2:B5").clear(ExcelScript.ClearApplyTo.contents); }
To delete the entire cells (do not confuse with rows), not only the contents:
function main(workbook: ExcelScript.Workbook) { //delete the cells A2:A5 and move the other cells up workbook.getWorksheet('Sheet2').getRange("A2:A5").delete(ExcelScript.DeleteShiftDirection.up); //delete the cells A2:A5 and move the other cells to left workbook.getWorksheet('Sheet2').getRange("A2:A5").delete(ExcelScript.DeleteShiftDirection.left); }
To delete entire columns and/or rows:
function main(workbook: ExcelScript.Workbook) { //deleting columns A workbook.getWorksheet('Sheet2').getRange("A:A").delete(ExcelScript.DeleteShiftDirection.left); //deleting rows 1 workbook.getWorksheet('Sheet2').getRange("1:1").delete(ExcelScript.DeleteShiftDirection.up); }
To replace a content of a cell by another value:
function main(workbook: ExcelScript.Workbook) { //replace old by new for the column A workbook.getWorksheet('Sheet2').getRange("A:A").replaceAll("old","new",{completeMatch:true, matchCase:false}); }
To find a value in a cell:
function main(workbook: ExcelScript.Workbook) { //search old in the column A workbook.getWorksheet('Sheet2').getRange("A:A").find("old",{completeMatch:true, matchCase:false}); }
To insert columns and rows:
function main(workbook: ExcelScript.Workbook) { //insert a cell in A2 and move the other cells down workbook.getWorksheet('Sheet2').getRange("A2").insert(ExcelScript.InsertShiftDirection.down); //insert a cell in A2 and move the other cells to right workbook.getWorksheet('Sheet2').getRange("A2").insert(ExcelScript.InsertShiftDirection.right); //insert a new column in column A workbook.getWorksheet('Sheet2').getRange("A:A").insert(ExcelScript.InsertShiftDirection.right); //insert a new row in the row 4 workbook.getWorksheet('Sheet2').getRange("4:4").insert(ExcelScript.InsertShiftDirection.down); }
Once the script will be done, I can create a button not only to share with my colleagues but also for a quicker access (read How to create a button and associated it to an Office Script in an excel report). Additionally, I can use Power Automate Cloud to automate my script with different actions. For that, I will need to upload it online (sharepoint, onedrive, etc.) and click on “automate work” (this option is inactive if the excel file is not in the cloud):
To know more functions, check this official Microsoft Office Script website.
Interesting Topics
-
Be successfully certified ITIL 4 Managing Professional
Study, study and study, I couldn’t be successfully certified without studying it, if you are interested...
-
Be successfully certified ITIL 4 Strategic Leader
With my ITIL 4 Managing Professional certification (ITIL MP) in the pocket, it was time to go for the...
-
Hide visual and change background color based on selection
Some small tricks to customize the background colour of a text box...
-
Stacked and clustered column chart or double stacked column chart
In excel, I use a lot the combination of clustered and stacked chart...