【发布时间】:2021-04-11 00:49:55
【问题描述】:
我是 Apps 脚本的新手。我的背景是大型机,所以类 Java 语言的术语和概念对我来说有点陌生。这是我的情况。我有几个非常相似的谷歌电子表格,但彼此略有不同。我想使用相同的独立 Apps 脚本来重置这些 Google 电子表格中的字段。我编写的脚本在容器绑定时运行良好,但是当我将它放入 MyDrive 中的 Google Apps 脚本(项目?)文件并尝试使用分配给函数名称 resetRounds 的按钮从那里执行它时,我得到“脚本未找到”错误。我不想在每个电子表格中都有相同脚本的副本。我必须做任何特别的事情来将项目文件连接到电子表格吗?什么可能导致“未找到”情况?任何帮助或建议将不胜感激。
这是存在于 Google Apps 脚本项目文件中的脚本。
function resetRounds() {
var ss = SpreadsheetApp.getActive();
var ssName = ss.getName();
var shRounds = ss.getSheetByName("Rounds");
var shLog = ss.getSheetByName('Print-Log').getName();
var shDDList = ss.getSheetByName("DD-Lists");
// Restore SVE team name from DD-Lists sheet
var source = shDDList.getRange("A3"); /* Team name should be in cell A3 */
var destn = shRounds.getRange("D3"); /* copy it to Team Name cell */
source.copyTo(destn, {contentsOnly:true}); /* copy contents only, not the formatting */
// Reset visitor team name and Captains to trigger cell conditional formatting */
destn = shRounds.getRange("L3").activate().setValue('None');
destn = shRounds.getRange("N4").activate().setValue('None');
// Set the date to 1/1/2001 to trigger conditional formatting
// Week number will show as blank (0) when special date 1/1/2001 is used
source = shDDList.getRange("A51"); /* Special date should be in cell A51 */
destn = shRounds.getRange("Q3"); /* Only the first cell needs to be selected */
source.copyTo(destn, {contentsOnly:true}); /* copy contents only, not the formatting */
// Restore times from DD-Lists sheet
source = shDDList.getRange("A5"); /* Round 1 time should be in cell A5 */
destn = shRounds.getRange("C6"); /* copy it to Round 1 cell */
source.copyTo(destn, {contentsOnly:true}); /* copy contents only, not the formatting */
source = shDDList.getRange("A6"); /* Round 2 time should be in cell A6 */
destn = shRounds.getRange("L6"); /* copy it to Round 2 cell */
source.copyTo(destn, {contentsOnly:true}); /* copy contents only, not the formatting */
// Reset court numbers
ss.getRange("B8:B13").activate().setValue('Y'); /* Court number 1 */
ss.getRange("B15:B20").activate().setValue('2'); /* Court number 2 */
ss.getRange("B22:B27").activate().setValue('3'); /* Court number 3 */
ss.getRange("B29:B34").activate().setValue('4'); /* Court number 4 */
ss.getRange("B36:B41").activate().setValue('5'); /* Court number 5 */
ss.getRange("B43:B48").activate().setValue('6'); /* Court number 6 */
// Clear round 1 and round 2 player names
ss.getRange("C8:C48").activate().clear({contentsOnly: true, skipFilteredRows: true});
ss.getRange("E8:E48").activate().clear({contentsOnly: true, skipFilteredRows: true});
ss.getRange("L8:L48").activate().clear({contentsOnly: true, skipFilteredRows: true});
ss.getRange("N8:N48").activate().clear({contentsOnly: true, skipFilteredRows: true});
// Clear round 1 and round 2 scores
ss.getRange("G8:H48").activate().clear({contentsOnly: true, skipFilteredRows: true});
ss.getRange("P8:Q48").activate().clear({contentsOnly: true, skipFilteredRows: true});
// Clear scorekeeper names
ss.getRange("C56:E59").activate().clear({contentsOnly: true, skipFilteredRows: true});
ss.getRange("C61:E62").activate().clear({contentsOnly: true, skipFilteredRows: true});
/* Reset cursor to Visitor */
ss.getRange('L3:N3').activate();
}
【问题讨论】:
-
我相信电子表格上的按钮只能连接到电子表格中包含的项目之一中的脚本,而不是独立项目
-
你也许可以使用图书馆。
-
您能解释一下“项目”是什么以及它与电子表格的关系吗?我有一个使用独立脚本创建的 Google Apps 脚本文件。这和图书馆一样吗?
-
一个项目包含脚本。如果一个项目是独立的,那么它没有容器。如果一个项目有一个容器,那么它可以附加到幻灯片、文档或电子表格中,您可以使用引用 getActive 的方法。使用独立脚本,他们必须打开文档和电子表格 byid 或 url。
标签: google-apps-script google-sheets