【发布时间】:2020-06-25 15:04:16
【问题描述】:
“A”列有一百个不同的谷歌表格或 ID 的列表 我有我创建的宏的应用脚本,需要在所有 100 个谷歌表格的“标签 2”上运行
是否可以创建一个脚本来遍历每个文件并应用宏? 如果没有,是否有一种快速简便的方法可以让我访问每个 Google 工作表中的宏?
这是所要求的宏代码 - 更改了一些信息。
function Combined () {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('AW:AW').activate();
spreadsheet.getActiveSheet().showColumns(47, 1);
spreadsheet.getRange('AV2').activate();
spreadsheet.getCurrentCell().setFormula('=ifs(H2="Brossard", "111",H2="Calgary", "222",H2="Edmonton", "333",H2="Gatineau", "444",H2="Halifax", "555",H2="London", "666",H2="Montreal", "777",H2="Oakville", "888",H2="Ottawa", "999",H2="Quebec", "111",H2="Regina", "222",H2="Saint John", "333",H2="Saskatoon", "444",H2="St. John\'s", "555",H2="Surrey", "666",H2="Toronto Downtown", "777",H2="Truro", "888",H2="Vancouver", "999",H2="Vaughan", "111",H2="Victoria", "222",H2="Waterloo", "333",H2="Windsor", "444",H2="Winnipeg", "555",H2="Waterloo", "666",H2="Windsor", "777",H2="Winnipeg", "888")');
spreadsheet.getActiveRange().autoFill(spreadsheet.getRange('AV2:AV1000'), SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
spreadsheet.getRange('AV:AV').activate();
spreadsheet.getActiveSheet().hideColumns(spreadsheet.getActiveRange().getColumn(), spreadsheet.getActiveRange().getNumColumns());
spreadsheet.getRange('AW:AW').activate();
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('B:B').activate();
var allProtections = spreadsheet.getActiveSheet().getProtections(SpreadsheetApp.ProtectionType.RANGE);
var matchingProtections = allProtections.filter(function(existingProtection) {
return existingProtection.getRange().getA1Notation() == 'B:B';
});
var protection = matchingProtections[0];
protection.addEditors(['abc@abc.com', 'abc@abc.com']);
allProtections = spreadsheet.getActiveSheet().getProtections(SpreadsheetApp.ProtectionType.RANGE);
matchingProtections = allProtections.filter(function(existingProtection) {
return existingProtection.getRange().getA1Notation() == 'B:B';
});
protection = matchingProtections[0];
allProtections = spreadsheet.getActiveSheet().getProtections(SpreadsheetApp.ProtectionType.RANGE);
matchingProtections = allProtections.filter(function(existingProtection) {
return existingProtection.getRange().getA1Notation() == 'A:A';
});
protection = matchingProtections[0];
protection.addEditors(['abc@abc.com', 'abc@abc.com']);
allProtections = spreadsheet.getActiveSheet().getProtections(SpreadsheetApp.ProtectionType.RANGE);
matchingProtections = allProtections.filter(function(existingProtection) {
return existingProtection.getRange().getA1Notation() == 'A:A';
});
protection = matchingProtections[0];
spreadsheet.getRange('AW:AW').activate();
allProtections = spreadsheet.getActiveSheet().getProtections(SpreadsheetApp.ProtectionType.RANGE);
matchingProtections = allProtections.filter(function(existingProtection) {
return existingProtection.getRange().getA1Notation() == 'AV:AV';
});
protection = matchingProtections[0];
protection.addEditors(['abc@abc.com', 'abc@abc.com']);
allProtections = spreadsheet.getActiveSheet().getProtections(SpreadsheetApp.ProtectionType.RANGE);
matchingProtections = allProtections.filter(function(existingProtection) {
return existingProtection.getRange().getA1Notation() == 'AV:AV';
});
protection = matchingProtections[0];
spreadsheet.getRange('C1:AW1').activate();
allProtections = spreadsheet.getActiveSheet().getProtections(SpreadsheetApp.ProtectionType.RANGE);
matchingProtections = allProtections.filter(function(existingProtection) {
return existingProtection.getRange().getA1Notation() == 'C1:AV1';
});
protection = matchingProtections[0];
protection.addEditors(['abc@abc.com', 'abc@abc.com']);
allProtections = spreadsheet.getActiveSheet().getProtections(SpreadsheetApp.ProtectionType.RANGE);
matchingProtections = allProtections.filter(function(existingProtection) {
return existingProtection.getRange().getA1Notation() == 'C1:AV1';
});
protection = matchingProtections[0];
}
【问题讨论】:
-
只有一个宏吗?如果有,请分享。
-
@Cooper 更新了!
-
哦,我在这里假设你是一名程序员,或者至少你渴望成为一名程序员。如果这不是真的,那么我很抱歉,我将继续讨论另一个问题。
-
问题是宏虽然易于创建,但作为脚本确实很糟糕。把它们变成工作脚本有点麻烦,但它会导致一个更有用的脚本。我怀疑如果您尝试自动化此脚本以使用 100 个电子表格运行,您最终将不得不以任何方式重写它,因为您可能会超时。
-
学习资源见tag info page
标签: google-apps-script google-sheets google-sheets-formula google-sheets-api google-sheets-macros