簡體   English   中英

Google App 腳本:在第 1 列中與第 2 列中的用戶共享電子表格

[英]Google App Script: Share spreadsheets in Column 1, with Users in Column 2

客觀的

在腳本運行時,授予對所有電子郵件(B 列)、所有電子表格(A 列)的查看訪問權限

電子表格看起來像:

工作表網址 電子郵件
https://docs.google.com/spreadsheets/d/1 Bob@email.com
https://docs.google.com/spreadsheets/d/2 約翰@email.com

劇本

function setSheetPermissions(){
  //Spreadsheet that contains the Spreadsheet URL's & Emails.
  var ss= SpreadsheetApp.openById('spreadsheetID')
  
  // Sheet name that contains the URL's & Emails
  var sheet = ss.getSheetByName("Sheet1")
  
  // Get the values of all the URL's in column A
  var getSheetURLs = sheet.getRange("A2:A50").getValues();

  // Get the values of all the emails in column B
  var getEmails = sheet.getRange("B2:B50").getValues();
  
  for ( i in getEmails)
     getSheetURLs.addViewer(getEmails[i][0])

}

問題/錯誤

getSheetIDs.addViewer 不是 function(第 26 行,文件“代碼”)

第 26 行: getSheetURLs.addViewer(getEmails[i][0])

我究竟做錯了什么?

修改點:

  • 您可以從“A”和“B”列中檢索這兩個值。
  • 在您的腳本中, getSheetURLs是一個二維數組。 在這種情況下,不能直接使用addViewer方法。 我認為這是您的錯誤消息的原因。

當這些點反映在你的腳本中時,它變成如下。

修改后的腳本:

從:

// Get the values of all the URL's in column A
var getSheetURLs = sheet.getRange("A2:A50").getValues();

// Get the values of all the emails in column B
var getEmails = sheet.getRange("B2:B50").getValues();

for ( i in getEmails)
   getSheetURLs.addViewer(getEmails[i][0])

至:

var values = sheet.getRange("A2:B" + sheet.getLastRow()).getValues();
values.forEach(([sheetUrl, email]) => {
  if (sheetUrl && email) SpreadsheetApp.openByUrl(sheetUrl + "/edit").addViewer(email);
});
  • 從您顯示的示例電子表格中,我了解到您的電子表格 URL 就像https://docs.google.com/spreadsheets/d/### 在這種情況下,不能直接使用openByUrl 所以我添加了/edit 如果您顯示的電子表格 URL 與示例電子表格不同,請提供您的示例電子表格 URL。 通過這個,我想修改腳本。

  • 如果您的實際電子表格 URL 都是https://docs.google.com/spreadsheets/d/###https://docs.google.com/spreadsheets/d/###/edit ,您還可以使用SpreadsheetApp.openByUrl(sheetUrl + (sheetUrl.includes("/edit")? "": "/edit")).addViewer(email)

參考:

暫無
暫無

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

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