Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script GoogleSheets:是否可以保护一系列单元格,使其只能用脚本进行修改?_Google Apps Script_Google Sheets - Fatal编程技术网

Google apps script GoogleSheets:是否可以保护一系列单元格,使其只能用脚本进行修改?

Google apps script GoogleSheets:是否可以保护一系列单元格,使其只能用脚本进行修改?,google-apps-script,google-sheets,Google Apps Script,Google Sheets,嗨,我有一个共享的谷歌表,当按下按钮时,它会输入日期和时间。但是,一旦输入,我不希望用户能够修改日期。我知道我可以保护一系列单元格,但在这种情况下,我仍然需要宏/脚本才能输入时间 这可能吗 谢谢。我相信你的目标如下 function runWithWorkaround() { var lock = LockService.getDocumentLock(); if (lock.tryLock(10000)) { try { const url =

嗨,我有一个共享的谷歌表,当按下按钮时,它会输入日期和时间。但是,一旦输入,我不希望用户能够修改日期。我知道我可以保护一系列单元格,但在这种情况下,我仍然需要宏/脚本才能输入时间

这可能吗


谢谢。

我相信你的目标如下

  function runWithWorkaround() {
    var lock = LockService.getDocumentLock();
    if (lock.tryLock(10000)) {
      try {
        const url = "https://script.google.com/macros/s/###/exec";
        UrlFetchApp.fetch(url, {headers: {authorization: `Bearer ${ScriptApp.getOAuthToken()}`}});
      } catch(e) {
        throw new Error(e);
      } finally {
        lock.releaseLock();
      }
    }
    // DriveApp.getFiles() // This comment line is used for automatically detecting the scope for using Web Apps.
  }
  • 有您(电子表格的所有者)和用户
  • 用户通过单击按钮运行脚本,脚本将日期值放入单元格。此时,您希望已经保护了用于向用户输入日期值的单元格
  • 但是,当用户单击按钮时,您希望将日期值放入受保护的单元格中
  • 在您的情况下,可以使用谷歌应用程序脚本
从中,我可以确认我的理解是正确的

问题和解决方法: 当通过单击Google电子表格上的按钮运行脚本时,脚本将作为单击按钮的用户运行。因此,需要以用户身份对作用域进行授权。我想这可能是您回答的答案
我很困惑,因为脚本是否与单击按钮的人具有相同的权限?

在这种情况下,当脚本将值放入受所有者保护的单元格时,会出现类似
的错误,您试图编辑受保护的单元格或对象。如果需要编辑,请联系电子表格所有者以删除保护。
。因此,为了避免此错误,我们认为当脚本作为所有者运行时,将避免此问题

在这个回答中,我想提出一个解决办法。此解决方法如下所示

  function runWithWorkaround() {
    var lock = LockService.getDocumentLock();
    if (lock.tryLock(10000)) {
      try {
        const url = "https://script.google.com/macros/s/###/exec";
        UrlFetchApp.fetch(url, {headers: {authorization: `Bearer ${ScriptApp.getOAuthToken()}`}});
      } catch(e) {
        throw new Error(e);
      } finally {
        lock.releaseLock();
      }
    }
    // DriveApp.getFiles() // This comment line is used for automatically detecting the scope for using Web Apps.
  }
当用户通过单击电子表格上的按钮运行脚本时,它将使用Web应用程序以所有者身份运行脚本。使用此变通方法时,请执行以下流程

用法: 1.准备电子表格。 请创建新的电子表格并创建一个按钮,并将
runWithWorkaround
的功能分配给该按钮。并且,请作为唯一所有者的用户保护单元格“A1”。在此示例中,目标单元格为“A1”

2.准备脚本。 请将以下脚本复制并粘贴到Google电子表格的脚本编辑器中。并且,请设置带有按钮的工作表名称

// This function puts a date to cell "A1".
function putValue() {
  SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("A1").setValue(new Date());
}

// This function is for Web Apps.
function doGet() {
  putValue();
  return ContentService.createTextOutput();
}

// This function is used for testing "without using this workaround.".
function runWithoutWorkaround() {
  putValue();
}

// This function is used for testing "with using this workaround.".
function runWithWorkaround() {
  const url = "https://script.google.com/macros/s/###/exec";  // <--- Please replace this URL with your Web Apps URL.
  UrlFetchApp.fetch(url, {headers: {authorization: `Bearer ${ScriptApp.getOAuthToken()}`}});
  // DriveApp.getFiles() // This comment line is used for automatically detecting the scope "https://www.googleapis.com/auth/drive.readonly" for using Web Apps.
}
  • 上面的示例脚本是用于解释此变通方法的简单示例脚本。请小心这个。因此,如果您使用此变通方法,请使用此变通方法修改您的实际脚本

  • 参考资料:

    例如,在您的情况下,有您(电子表格的所有者)和用户。用户通过单击按钮运行脚本,脚本将日期值放入单元格。此时,您希望已经保护了用于向用户输入日期值的单元格。但是,当用户单击按钮时,您希望将日期值放入受保护的单元格中。我的理解正确吗?在这种情况下,我认为需要使用GoogleApps脚本。这个怎么样?@Tanaike是的,这是正确的,但是我很困惑,因为脚本是否与单击按钮的人具有相同的权限?谢谢您的回复。从你的答复中,我提出了一个变通办法作为答案。你能确认一下吗?如果这不是你期望的方向,我道歉。那太不可思议了。谢谢你在这方面的帮助。我认为如果第22条军规被允许的话,这是不可能的。@Ahdee谢谢你的回答。我很高兴。也谢谢你。