简体   繁体   English

Google Apps脚本/ Google Sheet错误? 引用命名范围的复制/重新设置数据验证不适用于重复的图纸

[英]Google Apps Script/Google Sheet Bug? Copying/Re-Setup Data Validation refering to named range does not work on duplicated Sheets

SEE THIS EXAMPLE HERE 在此查看此示例

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));
}
  • You want to copy the Data Validation including the named range as the values. 您要复制数据验证,包括命名范围作为值。

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()方法?

Modified script: 修改后的脚本:

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

Note: 注意:

  • Please test the script for both sheets. 请测试两个工作表的脚本。
  • If you want to copy both the value and Data Validation, please use source.copyTo(target) instead of source.copyTo(target, SpreadsheetApp.CopyPasteType.PASTE_DATA_VALIDATION) . 如果要复制值和数据验证,请使用source.copyTo(target)而不是source.copyTo(target, SpreadsheetApp.CopyPasteType.PASTE_DATA_VALIDATION)

Reference: 参考:

暂无
暂无

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

相关问题 谷歌表格脚本:获取表格命名范围已打开 - Google Sheets Script: Get Sheet a Named Range is On Google Sheets Script - 将数据范围从一张表复制到另一张表 - 整个范围不复制 - Google Sheets Script - Copying data range from one sheet to another - whole range not copying 将数据表1复制到工作表2,以便您可以对两个表进行排序和编辑(Google Apps脚本?) - Copying Data Sheet1 to Sheet2 so you can sort & edit both sheets (google apps script?) 应用程序脚本和谷歌表格,更改命名范围的正确方法? - apps script and google sheets, proper way to change range of named range? Google Sheets Apps 脚本,从命名范围返回随机项目 - Google Sheets Apps Script, return random item from named range 搜索Google应用脚本以迭代工作表文件夹并将一系列数据附加到合并的主工作表中 - Searching for a Google-apps-script to iterate through a folder of sheets and append a range of data to a consolidated master sheet 如何在 Google 电子表格中以编程方式(在 Google 应用程序脚本中)为数据验证设置命名范围? - How to set a named range for a data validation programmatically (in Google apps script) in a Google spreadsheet? 使用脚本将数组中的数据复制到Google表格中的某个范围 - Copying data from an array into a range in Google Sheets using script 使用Google Apps脚本复制数据 - Data copying with Google Apps Script 用于将多个工作表中的阵列复制到单个“状态摘要”表单上的Google Apps脚本 - Google Apps Script for copying an array from multiple sheets onto a single Status Summary sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM