如何使用Google Apps脚本Range.setDataValidation [英] How to use Google Apps Script Range.setDataValidation
问题描述
我想限制可以在Google Spreadsheet中选择的可能值.通过UI可以轻松做到这一点,但我想在Apps脚本中完成此操作.当我输入以下内容时:
I'd like to limit the possible values that can be selected in a Google Spreadsheet. This is easily doable via the UI, but I'd like to accomplish this in an Apps Script. When I put in the following:
var sheet = // get a sheet from somewhere
sheet.getRange(row, column, numRows, numColumns).setDataValidation(dataValidation);
编辑器自动完成"setDataValidation"方法.弹出帮助说它需要一个"DataValidation"类.但是,我找不到此类或方法的任何文档.
The editor autocompletes the "setDataValidation" method. The pop-up help says it takes a "DataValidation" class. 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?
谢谢
推荐答案
我认为它是一个新功能.您可以通过执行以下操作获得DataValidation:
I think its a new Feature. You can get a DataValidation by doing:
var r = SpreadsheetApp.openById("...").getActiveRange();
var dv = r.getDataValidation();
在dv.
的弹出窗口中,您可以看到以下功能:
In the popup for 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);
这篇关于如何使用Google Apps脚本Range.setDataValidation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!