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 如何在应用程序脚本中动态保护google sheets中的特定范围?_Google Apps Script_Google Sheets - Fatal编程技术网

Google apps script 如何在应用程序脚本中动态保护google sheets中的特定范围?

Google apps script 如何在应用程序脚本中动态保护google sheets中的特定范围?,google-apps-script,google-sheets,Google Apps Script,Google Sheets,我知道如何通过应用程序脚本保护谷歌表单中的范围,但我想动态保护特定的范围,我们如何做到这一点 我曾尝试在数据来自BigQuery时设置保护,但在使用过滤器后,它不允许更新当前工作表,因为它受保护 var spreadsheet = SpreadsheetApp.getActive(); spreadsheet.getRange('A1:L28').activate(); var protection = spreadsheet.getRange('A9').protect(); pr

我知道如何通过应用程序脚本保护谷歌表单中的范围,但我想动态保护特定的范围,我们如何做到这一点

我曾尝试在数据来自BigQuery时设置保护,但在使用过滤器后,它不允许更新当前工作表,因为它受保护

var spreadsheet = SpreadsheetApp.getActive();
  spreadsheet.getRange('A1:L28').activate();
  var protection = spreadsheet.getRange('A9').protect();
  protection.removeEditors(['nikhil.doda@mediaagility.com']);

我希望只有在数据到达google电子表格后,输出才会受到保护,然后才能过滤数据并更改表格中的数据,而不会出现受保护的表格错误

如果您从外部将Bigquery数据插入google电子表格,则无法使用应用程序脚本检测更新事件

要插入BigQuery数据时,您可以执行以下操作:
  • 移除所有保护装置
  • 从BigQuery检索数据并使用应用程序脚本粘贴到电子表格中
  • 设置新的保护
  • 样本:

    function myFunction() {
      var spreadsheet = SpreadsheetApp.getActive();
      //remove all protections
      spreadsheet.getProtections(SpreadsheetApp.ProtectionType.RANGE).forEach(function(e){e.remove()});
      //not sure what you want to do with the following line
      spreadsheet.getRange('A1:L28').activate();
      //  Retrieve your Bigquery data and proceed as required
      queryResults = BigQuery.Jobs.query(queryRequest, projectNumber);  
      ...  
      //After finishing the query, protect the range
      //provided you want to protect only cell 'A9' in the active sheet
      var protection = spreadsheet.getActiveSheet().getRange('A9').protect();
      protection.removeEditors(['nikhil.doda@mediaagility.com']);
    }