Google Script:设置单元格值+提示消息框

时间:2017-10-02 15:34:49

标签: google-apps-script google-sheets

我正在编写下面的脚本(请记住,它还远未完成):

function Import() {
  var dealCells = getRange('Deals');

  var blank = new Array ("");  

  if (blank.indexOf(dealCells.getValue()) != -1) {
    if (Browser.msgBox('You have not entered all the Deal IDs, do you want to continue anyway?', Browser.Buttons.YES_NO) == 'no') {
      Browser.msgBox('Please fill in all the Deal IDs')

      if (blank.indexOf(dealCells.getValue()) == -1) {
        var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Worksheet");
        var cell = sheet.getRange(2,12);
        cell.setValue("IMPORT");
      }
    }
  }
}

我试图确定工作表中的A4:A23范围内是否有空单元格'时事通讯'。

  • 如果有空单元格:会提示是/否消息框

    • 如果是:继续将单元格值设置为' IMPORT'并且无需进一步操作即可完成脚本
    • 如果否:提示第二个消息框并完成脚本而不采取任何进一步操作

1 个答案:

答案 0 :(得分:0)

假设dealCellsA4:A23

function Import() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.getSheetByName('Newsletter');
  // Get range values
  var dealCells = sh.getRange("A4:A23").getValues() 
  // Flatten array for `indexOf()`
  var blank = dealCells.join().split(',');

  if (blank.indexOf("") != -1) { // Check if there is a blank cell
     // If a blank cell exist
    var response = Browser.msgBox('You have not entered all the Deal IDs, do you want to continue anyway?', Browser.Buttons.YES_NO)

    // Handle the msgBox response
    if (response == "yes") { 
      var value = Browser.msgBox('Please fill in all the Deal IDs')
    } else { //response == no
      ...
    }

  } else { // no blank cell
    ...
  }
...
}

修改您还可以getRangeByName()

var ss = SpreadsheetApp.getActiveSpreadsheet(); var dealCells = ss.getRangeByName('Deals').getValues();