调整应用程序脚本代码行

时间:2016-02-07 15:04:55

标签: google-apps-script

需要调整一行代码。现有工作代码的附加图像,并且在我认为修改应该去的灰色突出显示。代码将像" OR"在Excel中的功能。感谢。Screen shot of code

function resetAll(e){
    var optionAB = SpreadsheetApp.newDataValidation()
            .requireValueInList(['Pin', 'Tie Clip'], true).build();
    var optionsABC = SpreadsheetApp.newDataValidation()
            .requireValueInList(['Pin', 'Tie Clip', 'Magnet'], true).build();
    var ss = e.source || SpreadsheetApp.getActiveSheet();
    var dataRange = ss.getDataRange()
    var values = dataRange.getValues();
    Logger.log('hello');
    for (var i = 19 ; i < values.length ; i++){
        var affectedCell = dataRange.offset(i,5,1,1);  // two steps to the right
        if(values[i][3] == '') {
            Logger.log(values[i])
            Logger.log(affectedCell.getValue())
            if (affectedCell.getValue() !== 'Pin')
                affectedCell.setValue('');    // reset if current choice is no longer legal
            affectedCell.setDataValidation(optionAB);
        }
        else {
            affectedCell.setDataValidation(optionsABC);      
        }
    }
}

1 个答案:

答案 0 :(得分:0)

使用OR进行的两次负面比较不会以您期望的方式工作,而对另一种方式的正面效果将是负面的 - 所以现实地,您将始终获得True响应并且条件块将执行。

如果你要检查的是'Pin''Tie Clip'都不匹配,那么它就是你要找的AND

function resetAll(e) {
  var optionAB = SpreadsheetApp.newDataValidation().requireValueInList(['Pin', 'Tie Clip'], true).build(),
      optionsABC = SpreadsheetApp.newDataValidation().requireValueInList(['Pin', 'Tie Clip', 'Magnet'], true).build(),
      ss = (e.source || SpreadsheetApp.getActiveSheet()),
      dataRange = ss.getDataRange(),
      values = dataRange.getValues(),
      affectedCell,
      i;

  for (i = 19 ; i < values.length ; i += 1){
    affectedCell = dataRange.offset(i,5,1,1);  // two steps to the right
    if(values[i][3] == '') {
      Logger.log(values[i])
      Logger.log(affectedCell.getValue())
      if (affectedCell.getValue() !== 'Pin' && affectedCell.getValue() !== 'Tie Clip') {
        affectedCell.setValue('');    // reset if current choice is no longer legal
        affectedCell.setDataValidation(optionAB);
      }
      else {
        affectedCell.setDataValidation(optionsABC);      
      }
    }
  }
}