简体   繁体   English

Google App 脚本:在第 1 列中与第 2 列中的用户共享电子表格

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

Objective客观的

On script run, give view access for all of the emails (column B), to all of the spreadsheets (column A)在脚本运行时,授予对所有电子邮件(B 列)、所有电子表格(A 列)的查看访问权限

The Spreadsheet looks like:电子表格看起来像:

Sheet URL's工作表网址 Emails电子邮件
https://docs.google.com/spreadsheets/d/1 https://docs.google.com/spreadsheets/d/1 Bob@email.com Bob@email.com
https://docs.google.com/spreadsheets/d/2 https://docs.google.com/spreadsheets/d/2 John@email.com约翰@email.com

The script剧本

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])

}

Problem / Error问题/错误

getSheetIDs.addViewer is not a function (line 26, file "Code") getSheetIDs.addViewer 不是 function(第 26 行,文件“代码”)

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

What am I doing wrong?我究竟做错了什么?

Modification points:修改点:

  • You can retrieve both values from the columns "A" and "B".您可以从“A”和“B”列中检索这两个值。
  • In your script, getSheetURLs is a 2 dimensional array.在您的脚本中, getSheetURLs是一个二维数组。 In this case, the method addViewer cannot be directly used.在这种情况下,不能直接使用addViewer方法。 I thought that this is the reason for your error message.我认为这是您的错误消息的原因。

When these points are reflected in your script, it becomes as follows.当这些点反映在你的脚本中时,它变成如下。

Modified script:修改后的脚本:

From:从:

// 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])

To:至:

var values = sheet.getRange("A2:B" + sheet.getLastRow()).getValues();
values.forEach(([sheetUrl, email]) => {
  if (sheetUrl && email) SpreadsheetApp.openByUrl(sheetUrl + "/edit").addViewer(email);
});
  • From your showing sample Spreadsheet, I understood that your Spreadsheet URL is like https://docs.google.com/spreadsheets/d/### .从您显示的示例电子表格中,我了解到您的电子表格 URL 就像https://docs.google.com/spreadsheets/d/### In this case, openByUrl cannot be directly used.在这种情况下,不能直接使用openByUrl So I added /edit .所以我添加了/edit If your showing Spreadsheet URLs are different from your sample Spreadsheet, please provide your sample Spreadsheet URL.如果您显示的电子表格 URL 与示例电子表格不同,请提供您的示例电子表格 URL。 By this, I would like to modify the script.通过这个,我想修改脚本。

  • If your actual Spreadsheet URLs are both https://docs.google.com/spreadsheets/d/### and https://docs.google.com/spreadsheets/d/###/edit , you can also use SpreadsheetApp.openByUrl(sheetUrl + (sheetUrl.includes("/edit")? "": "/edit")).addViewer(email) .如果您的实际电子表格 URL 都是https://docs.google.com/spreadsheets/d/###https://docs.google.com/spreadsheets/d/###/edit ,您还可以使用SpreadsheetApp.openByUrl(sheetUrl + (sheetUrl.includes("/edit")? "": "/edit")).addViewer(email)

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM