[英]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])
我究竟做錯了什么?
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.