简体   繁体   English

Google Apps脚本数据验证代码不起作用

[英]Google Apps Script dataValidation code not working

Could you please help get the dataValidation part of the code working? 您能否帮助使代码的dataValidation部分正常工作? All the other parts of the code is working, only the dataValidation part is not working unfortunately. 代码的所有其他部分都正常工作,但是不幸的是只有dataValidation部分没有工作。

Here is the code: // "Ready Line" formats word in column J 下面是代码: //“就绪行”格式化J列中的单词

     if (r.getColumn() == 10 && e.value == "A Ready Line") {
      sheet.getRange(r.getRow(),r.getColumn()).setBackgroundColor("#FFFF00");
      sheet.getRange(r.getRow(),r.getColumn()).setFontColor("#38761d");
      var rule = SpreadsheetApp.newDataValidation().requireValueInRange(['B Hold', 'C Confirm']).build();
      sheet.getRange(r.getRow(),r.getColumn()).setDataValidation(rule);
   // Timestamp column CG  
      sheet.getRange(r.getRow(),r.getColumn()+75).setValue(new Date());
   // Clear columns MtoAA,   
      sheet.getRange(r.getRow(),r.getColumn()+3,1,15).clearContent();
        }

You should use requireValueInList instead of requireValueInRange: 您应该使用requireValueInList而不是requireValueInRange:

var rule = SpreadsheetApp.newDataValidation().requireValueInList(['B Hold', 'C Confirm']).build();

Also setBackgroundColor is depreciated. setBackgroundColor也被贬值。 As well you can simplify your code by using this: 您也可以使用以下方法简化代码:

var rule = SpreadsheetApp.newDataValidation().requireValueInList(['B Hold', 'C Confirm']).build();

sheet.getRange(r.getRow(),r.getColumn())
  .setBackground("#FFFF00")
  .setFontColor("#38761d")
  .setDataValidation(rule);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM