簡體   English   中英

谷歌表格使用應用腳本將用戶通過電子郵件包含在受保護的范圍內

[英]Google Sheet Include user by email in protected rang using app script

我正在使用以下腳本來保護 Google 表格(腳本在打開表格時運行)。 該保護將應用於除工作表所有者之外的所有用戶,即只有所有者才能訪問受保護的范圍。 我想更改保護標准,即基於電子郵件的所有者和特定用戶可以訪問受保護的范圍。

function installedOnOpen(e) {
const sheetNames = ["Sheet1"]; // Please set the sheet names you want to protect.
const sheets = e.source.getSheets().filter(s => 
sheetNames.includes(s.getSheetName()));
if (sheets.length == 0) return;
sheets.forEach(s => {
const p = s.getProtections(SpreadsheetApp.ProtectionType.RANGE);
if (p.length > 0) {
  p.forEach(pp => pp.remove());
}
const lastRow = s.getLastRow();
if (lastRow != 0) {
  const newProtect = s.getRange(1, 1, lastRow, s.getMaxColumns()).protect();
  newProtect.removeEditors(newProtect.getEditors());
  if (newProtect.canDomainEdit()) newProtect.setDomainEdit(false);
 }
 });
 }

上述任何幫助將不勝感激。

我相信你的目標如下。

  • 你想保護細胞。 在這種情況下,您希望按所有者和其他特定用戶編輯單元格。

在這種情況下,如何進行以下修改?

修改后的腳本:

function installedOnOpen(e) {
  const editors = ["###"]; // Added: Please set the email addresses you want to give the permission as the editor.

  const sheetNames = ["Sheet1"]; // Please set the sheet names you want to protect.
  const sheets = e.source.getSheets().filter(s => sheetNames.includes(s.getSheetName()));
  if (sheets.length == 0) return;
  sheets.forEach(s => {
    const p = s.getProtections(SpreadsheetApp.ProtectionType.RANGE);
    if (p.length > 0) {
      p.forEach(pp => pp.remove());
    }
    const lastRow = s.getLastRow();
    if (lastRow != 0) {
      const newProtect = s.getRange(1, 1, lastRow, s.getMaxColumns()).protect();
      newProtect.removeEditors(newProtect.getEditors());
      newProtect.addEditors(editors); // Added
      if (newProtect.canDomainEdit()) newProtect.setDomainEdit(false);
    }
  });
}
  • 在這個修改中,它假設函數installedOnOpen由可安裝的 OnEdit 觸發器運行。 請注意這一點。

參考:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM