如何使用Google App Script在多行中执行类似的功能?

时间:2016-12-16 14:33:54

标签: google-apps-script google-sheets

以下是我创建的脚本:

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)中执行此代码。我该怎么办?

1 个答案:

答案 0 :(得分:0)

尝试循环,

     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);
         }
      }

    }