客觀的
在腳本運行時,授予對所有電子郵件(B 列)、所有電子表格(A 列)的查看訪問權限
電子表格看起來像:
| 作業表網址 | 電子郵件 |
|---|---|
| https://docs.google.com/spreadsheets/d/1 | [email protected] |
| 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 不是函式(第 26 行,檔案“代碼”)
第 26 行: getSheetURLs.addViewer(getEmails[i][0])
我究竟做錯了什么?
uj5u.com熱心網友回復:
修改點:
- 您可以從“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).
參考:
- openByUrl(網址)
- 類電子表格的 addViewer(emailAddress)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/416112.html
標籤:
上一篇:將GoogleApp腳本中的日期格式化為YYYY-MM-DD
下一篇:如何使用for回圈加速if陳述句
