[英]Google Apps Script/Google Sheet Bug? Copying/Re-Setup Data Validation refering to named range does not work on duplicated Sheets
Situation: Data validation on cells that refer to a range via direct reference (A1Notation) and Data Validation on other cells that refer to a named range. 情况:通过直接引用(A1Notation)对引用范围的单元格进行数据验证,对引用命名范围的其他单元格进行数据验证。
Works both. 两者均可。
When duplicating the sheet, both still work. 复制工作表时,两者仍然有效。 However, when resetting the data validations manually to the same ranges or via my script, the data validation of the cell where the data validation refers via named range will not work and there is no way to set it up again by referring via named range either manually or by copying (.getDataValidation / .setDataValidation) via script.
但是,当手动将数据验证重置为相同的范围或通过我的脚本进行设置时,数据验证通过命名范围引用的单元格的数据验证将不起作用,也无法通过命名范围引用进行重新设置手动或通过脚本复制(.getDataValidation / .setDataValidation)。
Check out the example - run script function cp_dataValidation() either on Sheet1 and Copy of Sheet1 as an active sheet. 查看示例-在Sheet1上运行脚本函数cp_dataValidation(),然后将Sheet1的副本作为活动工作表运行。 Then click the data validation dropdowns in the cells.
然后单击单元格中的数据验证下拉菜单。
Eventually, my goal is to perform a copy of the data validation (referring to a named range) from one range to another on a duplicated sheet via script. 最终,我的目标是通过脚本在一个重复的工作表上从一个范围到另一个范围执行数据验证(指一个命名范围)的副本。
function cp_dataValidation() {
var sheet = SpreadsheetApp.getActiveSheet()
//cell with data validation referring to a named range
var named_range_tmpl = sheet.getRange("B2");
//cell with data validation referring to a range via A1Notation
var range_tmpl = sheet.getRange("C2");
//target cells to copy dataValidation to
var nr_target = sheet.getRange("D2");
var r_target = sheet.getRange("E2");
nr_target.setDataValidation(named_range_tmpl.getDataValidation());
r_target.setDataValidation(range_tmpl.getDataValidation());
Logger.log(JSON.stringify(named_range_tmpl.getDataValidation(), null, 2));
Logger.log(JSON.stringify(range_tmpl.getDataValidation(), null, 2));
}
If my understanding is correct, how about this workaround? 如果我的理解是正确的,该解决方法如何? It seems that
nr_target.setDataValidation(named_range_tmpl.getDataValidation())
cannot be used for the named range of other sheet. 看来
nr_target.setDataValidation(named_range_tmpl.getDataValidation())
不能用于其他工作表的命名范围。 So as a workaround, how about using the method of copyTo()
? 因此,作为一种解决方法,如何使用
copyTo()
方法?
var sheet = SpreadsheetApp.getActiveSheet();
var source = sheet.getRange("B2:C2");
var target = sheet.getRange("D2:E2");
source.copyTo(target, SpreadsheetApp.CopyPasteType.PASTE_DATA_VALIDATION);
target.clearContent(); // or target.clear({contentsOnly: true}); // Added
source.copyTo(target)
instead of source.copyTo(target, SpreadsheetApp.CopyPasteType.PASTE_DATA_VALIDATION)
. source.copyTo(target)
而不是source.copyTo(target, SpreadsheetApp.CopyPasteType.PASTE_DATA_VALIDATION)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.