Office Scripts Autofill value for a set range

311 views Asked by At

I have about 10,000 rows of data and need to autofill the value 1,2,3...64 for 143 rows. So "1" will be autofilled for 143 rows, then "2" for 143 rows and so forth until the value "64" or any value. This would be all in the same column.

I currently have the code below re-written over 30 times. but I know I can condense this and save time.

    let selectedSheet = workbook.getActiveWorksheet();
    // Set range F2 on selectedSheet
    selectedSheet.getRange("F2").setValue("1");
    // Auto fill range
    selectedSheet.getRange("F2").autoFill("F2:F143", ExcelScript.AutoFillType.fillDefault);
    // Set range F144 on selectedSheet
    selectedSheet.getRange("F144").setValue("2");
    // Auto fill range
    selectedSheet.getRange("F144").autoFill("F144:F285", ExcelScript.AutoFillType.fillDefault);
1

There are 1 answers

0
taller On BEST ANSWER
  • setValue can update multiple cells all at once.
  • A for loop is suitable for repeating the similar operation.
function main(workbook: ExcelScript.Workbook) {
  const rept_times = 143;
  const max_val = 64;
  let selectedSheet = workbook.getActiveWorksheet();
  for (let i = 0; i < max_val; i++) {
    let cell = selectedSheet.getCell(i * rept_times + 1, 5)
    cell.getAbsoluteResizedRange(rept_times, 1).setValue(i + 1)
  }
}