Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExcelScript: getNumberFormat from column A to C

I need to get the NumberFormat from an excel file (appr. 3000 lines) and I wrote a simple Script which copy it from column A to column C each row by row. I tried to use this script with Power Automate after 10 minutes.

I already set the timeout on 60 minutes, but it seems that my tenant configuration abort it always after exactly 10 minutes.

Here is my script:

    function main(workbook: ExcelScript.Workbook) {
    let sheet = workbook.getWorksheets()[0]

    let range = sheet.getUsedRange(true);
      let rows = range.getUsedRange(true).getRowCount();
      let cols = range.getUsedRange(true).getColumnCount();
      
      for (let row = 2; row <= rows; row++) {
        sheet.getRange("C" + row).setValue(sheet.getRange("A" + row).getNumberFormat())
      }
}

Is there an easier way to copy the NumberFormat from one column to another and does anybody know a better way?

like image 554
Paintitblack3k Avatar asked Apr 07 '26 22:04

Paintitblack3k


1 Answers

Please make sure when you ask these questions to put the Office-Scripts tag since this is Office Scripts. You don't need to loop to achieve this. You can use getNumberFormats() like in the code below.

function main(workbook: ExcelScript.Workbook) {
  let sheet = workbook.getWorksheets()[0];
  let rowCount = sheet.getUsedRange(true).getRowCount();
  let numFormats = sheet.getRange("A1:A" + rowCount).getNumberFormats();
  let newRng = sheet.getRange("C1:C" + numFormats.length).setValues(numFormats);
}

EDIT 4/23/21

If you just want to get the indent levels, then you could just insert this formula in column C rather than getting the number formats.

enter image description here

=IFERROR(LET(indentLevel,FIND("[",FILTER(A:A,A:A<>""),1),SWITCH(indentLevel,4,1,6,2,8,3,10,4,12,5)),"Other Formula Here")

Here is the Office Scripts

function main(workbook: ExcelScript.Workbook) {
  let sheet = workbook.getWorksheet("Sheet1");
  sheet.getRange("C1")
    .setFormulaLocal("=IFERROR(LET(indentLevel,FIND(\"[\",FILTER(A:A,A:A<>\"\"),1),SWITCH(indentLevel,4,1,6,2,8,3,10,4,12,5)),\"Other Formula Here\")");
}
like image 75
Ethan Avatar answered Apr 09 '26 14:04

Ethan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!