我有 8 个带有指定按钮的脚本,在访问电子表格时非常适合我。我想允许其他用户在单击关联按钮时运行所有脚本。我不是程序员,但只是想出了如何为自己做到这一点。我知道我可以部署一个网络应用程序并以我的名义执行,但不知道该怎么做。我已经在网络和本网站上搜索了答案,但是当我尝试部署时出现错误。尝试在脚本中使用
doGet(e)
但没有正确使用它。有人可以帮助我了解需要做什么才能允许其他用户单击按钮在我拥有的工作表电子表格上运行脚本吗?
第一个脚本:
function LockFriday() {
var protectRange = 'C8:D60';
var description = 'AA Lock Friday Cells';
var spreadsheet = SpreadsheetApp.openById('xxxxxxxxxxxxxxxxxxxxxxx');
spreadsheet.getRange(protectRange).activate();
spreadsheet.setCurrentCell(spreadsheet.getRange('D60'));
var p = spreadsheet.getProtections(SpreadsheetApp.ProtectionType.RANGE).find(e => e.getRange().getA1Notation() == protectRange && e.getDescription() == description);
if (p) return;
var protection = spreadsheet.getRange(protectRange).protect();
var all = protection.getEditors();
protection.setDescription(description).removeEditors(all);
};
关联的第二个脚本
function UnlockFriday() {
var spreadsheet = SpreadsheetApp.openById('xxxxxxxxxxxxxxxx');
spreadsheet.getRange('C8:D60').activate();
var allProtections = spreadsheet.getActiveSheet().getProtections(SpreadsheetApp.ProtectionType.RANGE);
var matchingProtection = allProtections.find(existingProtection => existingProtection.getRange().getA1Notation() == 'C8:D60');
if (!matchingProtection) return;
matchingProtection.remove();
}
您的脚本使用
SpreadsheetApp.openById
,因此编辑者还应该有权访问此方法引用的电子表格。
不管怎样,看看执行日志;他们可能会告诉您问题是什么。有关详细信息,请参阅 https://developers.google.com/apps-script/guides/support/troubleshooting
备注:
在电子表格中使用
getRange
时,请包含工作表名称,即假设工作表名称为 Sheet1
而不是 'C8:D60'
,请使用 'Sheet1!C8:D60'
。
对于电子表格所有者和编辑者来说,脚本的工作方式可能不同。当让脚本由编辑器运行时,避免使用
activate
和 getCurrentCell
等方法以及通过 openById
打开的电子表格。这些方法只能与 SpreadsheetApp.getActive()
一起使用。