另一个用户应该能够使用 google 应用脚本更新 google 电子表格中的受保护范围

Another user should be able to update the protected range in google spreadsheet using google app script

我与另一个用户共享了一个 sheet 的受保护范围,并希望他们提交数据,例如发票副本或一个 sheet 的特定单元格范围内的数据到另一个用户,使用SUBMIT 按钮,相应的脚本被分配到该按钮。

场景: 两个spreadsheets,即S1和S2,其中S2被保护,另一个用户想在he/she按下提交按钮后将数据从S1提交到S2。

function testing() {
  var ss = SpreadsheetApp.getActive();
  var s1 = ss.getSheetByName('S1');
  var s2 = ss.getSheetByName('S2');

  s1.getRange("A1:G1").copyTo(s2.getRange("A1:G1"));
  }

此外,我了解到这可以通过将脚本部署为 Web 应用程序来完成。我尝试通过进入 "Deploy as web app" 并将脚本设置为 运行 作为管理员(我)并且可以匿名访问。但是还是没有结果。

是否为 Web 应用程序部署放置了类似这样的东西:

function doGet(e){
  return testing(e);
}

function testing() {
  var ss = SpreadsheetApp.getActive();
  var s1 = ss.getSheetByName('S1');
  var s2 = ss.getSheetByName('S2');

  s1.getRange("A1:G1").copyTo(s2.getRange("A1:G1"));
}

Error Says: You are trying to edit a protected cell or object. Please contact the spreadsheet owner to remove protection if you need to edit.

谁能引导我完成整个过程,或者告诉我哪里出了问题?

当您 运行 调用 testing() 函数的代码时,使用的是当前用户拥有的任何帐户和权限。

您需要做的是使用 UrlFetchApp 调用 doGet() 这将 运行 作为发布应用程序的用户的脚本:

1:复制粘贴代码

function copyRange() {
    UrlFetchApp.fetch("--- Copy this URL from step 3 ---");
}

function doGet(e) {

    var ss = SpreadsheetApp.getActive();
    var s1 = ss.getSheetByName('Sheet1');
    var s2 = ss.getSheetByName('Sheet2');

    s1.getRange("A1").copyTo(s2.getRange("A1"));

}

2:单击'Publish' - 'Deploy' 设置新项目版本以我的身份执行并允许任何人,就像您在测试中所做的那样。点击'Update'

3: 复制当前网络应用 URL 并将其粘贴到 fetch() 保存脚本中。

现在任何用户都应该能够 运行 该功能,或者从第 3 步转到 URL 将触发该功能。