如何使用Google App脚本在许多行中执行相似的功能



以下是我创建的脚本:

function recordValue() {
  //read the current trigger price in 'Set Alert'!G2 and record it in cell P2
  var triggerPrice = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Set Alerts").getRange('G2').getCell(1, 1).getValue();
  var outputCell = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Set Alerts").getRange('P2').getCell(1, 1);
  if (triggerPrice != "") {
  outputCell.setValue(triggerPrice);
}
}

上面的脚本仅在第2行中可执行,但我想在其他行中执行此代码(3、4、5、6、7 ...... 11)。我该怎么做?

尝试循环,

     function recordValue() {
      //read the current trigger price in 'Set Alert'!G2 and record it in cell P2
      for(var rowIndex = 1 ; rowIndex < 11 ; rowIndex++) {
         var triggerPrice =   SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Set Alerts").getRange('G2').getCell(rowIndex, rowIndex).getValue();
         var outputCell = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Set Alerts").getRange('P2').getCell(rowIndex, rowIndex);
         if (triggerPrice != "") {
             outputCell.setValue(triggerPrice);
         }
      }
    }

最新更新