简体   繁体   English

如何使用Google Apps脚本Range.setDataValidation

[英]How to use Google Apps Script Range.setDataValidation

I'd like to limit the possible values that can be selected in a Google Spreadsheet. 我想限制可以在Google Spreadsheet中选择的可能值。 This is easily doable via the UI, but I'd like to accomplish this in an Apps Script. 通过UI可以轻松做到这一点,但我想在Apps脚本中完成此操作。 When I put in the following: 当我输入以下内容时:

var sheet = // get a sheet from somewhere
sheet.getRange(row, column, numRows, numColumns).setDataValidation(dataValidation);

The editor autocompletes the "setDataValidation" method. 编辑器将自动完成“ setDataValidation”方法。 The pop-up help says it takes a "DataValidation" class. 弹出帮助说它需要一个“ DataValidation”类。 However, I am unable to find any documentation for this class or this method. 但是,我找不到此类或该方法的任何文档。

Does anyone happen to know if this is actually supported? 有人碰巧知道这是否真正得到支持吗? And if so, how one might use it? 如果是这样,怎么使用呢?

Thank you 谢谢

I think its a new Feature. 我认为这是一个新功能。 You can get a DataValidation by doing: 您可以通过执行以下操作获取DataValidation:

var r = SpreadsheetApp.openById("...").getActiveRange();
var dv = r.getDataValidation();

In the popup for dv. dv.的弹出窗口中dv. you can see the following function: 您可以看到以下功能:

requireValuesInList(String[] values) : DataValidation
requireValuesInRange(Range rangeApi) : DataValidation
setHelpText(String helpText) : DataValidation
setShowDropDown(boolean showDropDown) : DataValidation

Then you need to set the validation back on the range: 然后,您需要将验证范围重新设置为:

r.setDataValidation(dv);

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

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