【发布时间】:2021-01-05 22:36:23
【问题描述】:
我共享了一个 Google 表格,但保护了某些范围不被编辑。现在,我无法从 App Script 以编程方式编辑范围,例如。为单元格设置一个值。问题:有没有办法保持范围保护(防止用户编辑受保护的范围)并通过脚本进行编辑?谢谢!
【问题讨论】:
标签: google-apps-script google-sheets
我共享了一个 Google 表格,但保护了某些范围不被编辑。现在,我无法从 App Script 以编程方式编辑范围,例如。为单元格设置一个值。问题:有没有办法保持范围保护(防止用户编辑受保护的范围)并通过脚本进行编辑?谢谢!
【问题讨论】:
标签: google-apps-script google-sheets
我没有找到您要查找的内容,但我想到了一种解决方法,该解决方法应该与您尝试执行的操作几乎相同。
尝试通过脚本删除范围保护,然后修改受保护范围并再次添加保护。
function main() {
// Remove protection before editing
removeProtectionToColumn();
// Call your function that modifies the protected range in between
modifySheetDataFunction();
// Add protection again
addProtectionToColumn();
}
function addProtectionToColumn() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getRange('A:A');
var protectSs = range.protect().setDescription('Protect column A');
var me = Session.getEffectiveUser();
protectSs.addEditor(me);
protectSs.removeEditors(protectSs.getEditors());
if (protectSs.canDomainEdit()) {
protectSs.setDomainEdit(false);
}
}
function removeProtectionToColumn() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var i = 0; i < protections.length; i++) {
if (protections[i].getDescription() == 'Protect column A') {
protections[i].remove();
}
}
}
这里应该有一个非常小的窗口时间,并且对于您尝试工作的任何事情都应该是可行的,除非您正在修改非常非常大的数据集。您可以随时与您的同事一起测试这种行为。
有关保护的更多详细信息,请参阅documentation
【讨论】: