Google apps script 使用onEdit触发器从不同的电子表格进行数据验证
我的源电子表格中有一个下拉列表,其中包含来自同一电子表格中另一个选项卡的范围的数据验证,以及使用以下脚本从另一个电子表格导入的数据 主电子表格速度非常慢,因为我有很多带有随脚本导入的数据的选项卡,因此如何在源电子表格上创建OneEdit触发器,以便在编辑其他电子表格时更新数据验证 剧本:Google apps script 使用onEdit触发器从不同的电子表格进行数据验证,google-apps-script,google-sheets,importrange,Google Apps Script,Google Sheets,Importrange,我的源电子表格中有一个下拉列表,其中包含来自同一电子表格中另一个选项卡的范围的数据验证,以及使用以下脚本从另一个电子表格导入的数据 主电子表格速度非常慢,因为我有很多带有随脚本导入的数据的选项卡,因此如何在源电子表格上创建OneEdit触发器,以便在编辑其他电子表格时更新数据验证 剧本: function importSheetA() { var values1 = SpreadsheetApp.openById('xxxxx'). getSheetByName('xxxxx').getRa
function importSheetA() {
var values1 = SpreadsheetApp.openById('xxxxx').
getSheetByName('xxxxx').getRange('A1:EO2000').getValues();
SpreadsheetApp.getActive().getSheetByName('masterop').
getRange(1,1,values1.length,values1[0].length).setValues(values1);
}
更新:
有了这个脚本,我可以在1页上显示下拉列表(“id1”)。
如何为许多电子表格ID执行此操作
function installTrigger() {
ScriptApp.newTrigger('changeValidationRule').forSpreadsheet(SpreadsheetApp.getActive()).onEdit().create();
}
function changeValidationRule(e) {
let values = e.source.getRange("A1:A3").getValues().flatMap(value => value); //flatting the row structures into a single dimension array
let sourceSheet= SpreadsheetApp.openById("id1").getSheets()[0];
let rule = SpreadsheetApp.newDataValidation().requireValueInList(values, true); // The boolean stands for `show dropdown`
sourceSheet.getRange(1, 1).setDataValidation(rule);
}
方法
您可以使用应用程序脚本onEdit()
installable触发器实现此行为
必须安装触发器,因为它需要授权才能访问源电子表格
触发器将放置在验证表中,并在手动更新时触发。触发器将运行一个函数,该函数将根据要考虑的值更新验证规则
为简单起见,我假设这些值位于验证表的一列中,范围为A1:A3
我将获取这些值,并在源电子表格的所需范围上构建验证规则
代码
免责声明
在您的示例中,您将这些值导入源电子表格中。您实际上可以引用源电子表格中的一个范围,但是我认为使用值列表更容易,因为我们直接在验证电子表格上工作。我将在下面提到这种方法
编辑
您可以在验证规则中使用的值项有限制。这个限额是500件。如果您需要验证500多个项目,除了将验证表中的所有值导入源电子表格之外,没有其他方法。您将能够在数据验证规则中引用此范围,并且可以构建一个脚本,在验证电子表格更改时更新此范围。这里有一个想法:
function installTrigger() {
ScriptApp.newTrigger('updateValidationRange').forSpreadsheet(SpreadsheetApp.getActive()).onEdit().create();
}
function updateValidationRange(e) {
let values = e.source.getRange("a-big-range-in-validation-spreadsheet").getValues();
// Initialize the variables
let ids = ['id1','id2','id3'];
// Loop through the ids and update the rule
ids.map(id => {
let sourceSheet= SpreadsheetApp.openById(id).getSheetByName("Validation Sheet"); // Be sure to change this name in order to reflect your validation sheet inside the source Spreadsheet
sourceSheet.getRange("the-copy-of-the-big-range-in-the-source-spreadsheet").setValues(values); // It's crucial this range is the same size than the one in the validation spreadsheet
});
}
工具书类
请澄清:“当编辑其他电子表格时?”您指的是哪个电子表格?您希望侦听源电子表格上的编辑事件,但数据验证基于来自其他工作表的导入值,对吗?所以你应该听数据验证值表上的编辑事件,对吗?是的,我想从只有在更新这些表时才导入的表中更新数据验证的数据。这很有效。现在,我需要几个运行验证的源代码表。怎么做?是否可以添加另一个字符串:let sourceSheet=SpreadsheetApp.openById(“源电子表格id”).getSheets()[0]??确切地只需选择所有的工作表,并在每个工作表中执行此操作。我现在在代码中有两个字符串:let sourceSheet=SpreadsheetApp.openById(“ID1”).getSheets()[0];让sourceSheet=SpreadsheetApp.openById(“ID2”).getSheets()[0];但是我有一个错误:SyntaxError:Identifier'sourceSheet'已经声明(riga 7,文件“Codice.gs”),您两次声明同一个变量。在计算机编程中,变量是用来存储信息的唯一标识符。在本例中,您使用Javascript编写应用程序脚本。为每个电子表格使用不同的变量名。我复制了这个函数changeValidationRule(e){let values=e.source.getRange(“A1:A3”).getValues().flatMap(value=>value);//将行结构平铺成一个一维数组let sourceSheet=SpreadsheetApp.openById(“源电子表格id”).getSheets()[0];let rule=SpreadsheetApp.newDataValidation().RequireWebEINList(values,true);//布尔值表示具有不同ID的
show下拉列表
sourceSheet.getRange(1,1).setDataValidation(rule);}。是否正确?
function installTrigger() {
ScriptApp.newTrigger('updateValidationRange').forSpreadsheet(SpreadsheetApp.getActive()).onEdit().create();
}
function updateValidationRange(e) {
let values = e.source.getRange("a-big-range-in-validation-spreadsheet").getValues();
// Initialize the variables
let ids = ['id1','id2','id3'];
// Loop through the ids and update the rule
ids.map(id => {
let sourceSheet= SpreadsheetApp.openById(id).getSheetByName("Validation Sheet"); // Be sure to change this name in order to reflect your validation sheet inside the source Spreadsheet
sourceSheet.getRange("the-copy-of-the-big-range-in-the-source-spreadsheet").setValues(values); // It's crucial this range is the same size than the one in the validation spreadsheet
});
}