Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Here is an example process for iterating through a particular column on a chosen worksheet using Office scripts:

  1. Open the desired worksheet using the getWorksheet function.
let sheet = context.workbook.worksheets.getItem("Sheet1");
  1. Select the column that you want to iterate through using the getRange function.
let range = sheet.getRange("A:A");
  1. Retrieve the values in the selected column using the getValues function.
let values = range.getValues();
  1. Use a for loop to iterate through each item in the column.
for(let i=0; i<values.length; i++){
  let item = values[i];
  // do something with the item
}
  1. Within the for loop, you can perform any necessary actions on the current item (e.g. write a value to a different column or compute a new value).
for(let i=0; i<values.length; i++){
  let item = values[i];
  sheet.getRange("B" + (i+1)).setValue(item * 2);
}
  1. You can also use conditional statements (e.g. if statements) to filter the items that you need to work with.
for(let i=0; i<values.length; i++){
  let item = values[i];
  if(item > 10){
    sheet.getRange("B" + (i+1)).setValue("High");
  } else {
    sheet.getRange("B" + (i+1)).setValue("Low");
  }
}