存取及修改受保護的範圍和工作表。受保護的範圍
儲存格範圍或已命名範圍受保護的工作表可能包含未受保護的區域。適用對象
透過舊版 Google 試算表建立的試算表,請使用
類別。
PageProtection
// Protect range A1:B10, then remove all other users from the list of editors. var ss = SpreadsheetApp.getActive(); var range = ss.getRange('A1:B10'); var protection = range.protect().setDescription('Sample protected range'); // Ensure the current user is an editor before removing others. Otherwise, if the user's edit // permission comes from a group, the script throws an exception upon removing the group. var me = Session.getEffectiveUser(); protection.addEditor(me); protection.removeEditors(protection.getEditors()); if (protection.canDomainEdit()) { protection.setDomainEdit(false); }
// Remove all range protections in the spreadsheet that the user has permission to edit. var ss = SpreadsheetApp.getActive(); var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE); for (var i = 0; i < protections.length; i++) { var protection = protections[i]; if (protection.canEdit()) { protection.remove(); } }
// Protect the active sheet, then remove all other users from the list of editors. var sheet = SpreadsheetApp.getActiveSheet(); var protection = sheet.protect().setDescription('Sample protected sheet'); // Ensure the current user is an editor before removing others. Otherwise, if the user's edit // permission comes from a group, the script throws an exception upon removing the group. var me = Session.getEffectiveUser(); protection.addEditor(me); protection.removeEditors(protection.getEditors()); if (protection.canDomainEdit()) { protection.setDomainEdit(false); }
方法
內容詳盡的說明文件
addEditor(emailAddress)
將指定的使用者新增至受保護工作表或範圍的編輯者清單。這個方法會
不自動授予使用者試算表本身的編輯權限。用途
此外,請呼叫 Spreadsheet.addEditor(emailAddress)
。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Adds an editor to the spreadsheet using an email address. // TODO(developer): Replace the email address with a valid email. ss.addEditor('cloudysanfrancisco@gmail.com'); // Gets a sheet by its name and protects it. const sheet = ss.getSheetByName('Sheet1'); const sampleProtectedSheet = sheet.protect(); // Adds an editor of the protected sheet using an email address. // TODO(developer): Replace the email address with a valid email. sampleProtectedSheet.addEditor('cloudysanfrancisco@gmail.com'); // Gets the editors of the protected sheet. const editors = sampleProtectedSheet.getEditors(); // Logs the editors' email addresses to the console. for (const editor of editors) { console.log(editor.getEmail()); }
參數
名稱 | 類型 | 說明 |
---|---|---|
emailAddress | String | 要新增的使用者電子郵件地址。 |
回攻員
Protection
:代表鏈結設定的物件。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
addEditor(user)
將指定的使用者新增至受保護工作表或範圍的編輯者清單。這個方法會
不自動授予使用者試算表本身的編輯權限。用途
此外,請呼叫 Spreadsheet.addEditor(user)
。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Gets a sheet by its name. const sheet = ss.getSheetByName('Sheet1'); // Protects the sheet. const sampleProtectedSheet = sheet.protect(); // Adds the active user as an editor of the protected sheet. sampleProtectedSheet.addEditor(Session.getActiveUser()); // Gets the editors of the protected sheet. const editors = sampleProtectedSheet.getEditors(); // Logs the editors' email addresses to the console. for (const editor of editors) { console.log(editor.getEmail()); }
參數
名稱 | 類型 | 說明 |
---|---|---|
user | User | 代表要新增的使用者。 |
回攻員
Protection
:代表鏈結設定的物件。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
addEditors(emailAddresses)
將指定使用者陣列新增至受保護工作表或範圍的編輯者清單。這個
方法,並不會自動授予使用者試算表本身的編輯權限。待辦事項
此外,也要呼叫 Spreadsheet.addEditors(emailAddresses)
。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Gets a sheet by its name. const sheet = ss.getSheetByName('Sheet1'); // Creates variables for the email addresses to add as editors. // TODO(developer): Replace the email addresses with valid ones. const TEST_EMAIL_1 = 'cloudysanfrancisco@gmail.com'; const TEST_EMAIL_2 = 'baklavainthebalkans@gmail.com'; // Protects the sheet. const sampleProtectedSheet = sheet.protect(); // Adds editors to the protected sheet using the email address variables. sampleProtectedSheet.addEditors([TEST_EMAIL_1, TEST_EMAIL_2]); // Gets the editors of the protected sheet. const editors = sampleProtectedSheet.getEditors(); // Logs the editors' email addresses to the console. for (const editor of editors) { console.log(editor.getEmail()); }
參數
名稱 | 類型 | 說明 |
---|---|---|
emailAddresses | String[] | 要新增的使用者電子郵件地址陣列。 |
回攻員
Protection
:代表鏈結設定的物件。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
addTargetAudience(audienceId)
將指定目標對像新增為受保護範圍的編輯者。
參數
名稱 | 類型 | 說明 |
---|---|---|
audienceId | String | 要新增的目標對象 ID。 |
回攻員
Protection
:代表鏈結設定的物件。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
canDomainEdit()
決定擁有試算表的網域內所有使用者能否編輯 受保護的範圍或工作表。如果使用者沒有編輯權限,就會擲回例外狀況 受保護的範圍或工作表。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Gets a sheet by its name. const sheet = ss.getSheetByName('Sheet1'); // Protects the sheet. const sampleProtectedSheet = sheet.protect(); // Logs whether domain users have permission to edit the protected sheet to the console. console.log(sampleProtectedSheet.canDomainEdit());
回攻員
Boolean
- 如果網域擁有試算表的所有使用者俱備以下權限:true
編輯受保護的範圍或工作表;false
則表示他們不知道。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
canEdit()
決定使用者是否有權編輯受保護的範圍或工作表。 試算表擁有者隨時可以編輯受保護的範圍和工作表。
// Remove all range protections in the spreadsheet that the user has permission to edit. var ss = SpreadsheetApp.getActive(); var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE); for (var i = 0; i < protections.length; i++) { var protection = protections[i]; if (protection.canEdit()) { protection.remove(); } }
回攻員
Boolean
— true
:使用者有權編輯保護範圍或工作表;如果不需要,則設為false
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getDescription()
取得受保護範圍或工作表的說明。如果未設定說明,這個方法 會傳回空字串。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Gets a sheet by its name. const sheet = ss.getSheetByName('Sheet1'); // Protects the sheet and sets the description. const sampleProtectedSheet = sheet.protect().setDescription('Sample sheet is protected'); // Gets the description of the protected sheet and logs it to the console. const sampleProtectedSheetDescription = sampleProtectedSheet.getDescription(); console.log(sampleProtectedSheetDescription);
回攻員
String
:受保護範圍或工作表的說明,如果沒有說明,則為空白字串
資源。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getEditors()
取得受保護範圍或工作表的編輯者清單。如果使用者有 無權編輯受保護的範圍或工作表。
// Protect the active sheet, then remove all other users from the list of editors. var sheet = SpreadsheetApp.getActiveSheet(); var protection = sheet.protect().setDescription('Sample protected sheet'); // Ensure the current user is an editor before removing others. Otherwise, if the user's edit // permission comes from a group, the script throws an exception upon removing the group. var me = Session.getEffectiveUser(); protection.addEditor(me); protection.removeEditors(protection.getEditors()); if (protection.canDomainEdit()) { protection.setDomainEdit(false); }
回攻員
User[]
:可編輯保護範圍或工作表的一組使用者
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getProtectionType()
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Gets a sheet by its name. const sheet = ss.getSheetByName('Sheet1'); // Protects the sheet. const sampleProtectedSheet = sheet.protect(); // Gets the type of the protected area. const protectionType = sampleProtectedSheet.getProtectionType(); // Logs 'SHEET'to the console since the type of the protected area is a sheet. console.log(protectionType.toString());
回攻員
ProtectionType
:保護區域的類型,可以是 RANGE
或 SHEET
。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getRange()
取得要保護的範圍。如果保護措施適用於工作表,而不是 範圍,這個方法會傳回橫跨整個工作表的範圍。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Gets a sheet by its name. const sheet = ss.getSheetByName('Sheet1'); // Gets the range 'A1:B10' of Sheet1. const range = sheet.getRange('A1:B10'); // Makes cells A1:B10 a protected range. const sampleProtectedRange = range.protect(); // Gets the protected ranges on the sheet. const protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE); // Logs the A1 notation of the first protected range on the sheet. console.log(protections[0].getRange().getA1Notation());
回攻員
Range
- 要保護的範圍。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getRangeName()
取得保護範圍的名稱 (如果與已命名範圍相關聯)。如果保護措施未與已命名範圍建立關聯,則傳回 null
。請注意,指令碼必須明確
呼叫 setRangeName(rangeName)
,將受保護的範圍與已命名範圍建立關聯;撥號中
Range.protect()
:從剛果的 Range
建立保護措施
只呼叫 setRangeName(rangeName)
不足以連結已命名範圍
具體做法是指示 Kubernetes 建立並維護
一或多個代表這些 Pod 的物件不過,在 Google 試算表 UI 中,透過已命名範圍建立受保護的範圍
因為系統會自動更新物件
// Protect a named range in a spreadsheet and log the name of the protected range. var ss = SpreadsheetApp.getActive(); var range = ss.getRange('A1:B10'); var protection = range.protect(); ss.setNamedRange('Test', range); // Create a named range. protection.setRangeName('Test'); // Associate the protection with the named range. Logger.log(protection.getRangeName()); // Verify the name of the protected range.
回攻員
String
:受保護的命名範圍名稱;如果保護範圍不是受保護範圍,則為 null
與已命名範圍相關聯
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getTargetAudiences()
傳回可以編輯受保護範圍的目標對象 ID。
回攻員
TargetAudience[]
:目標對象的 ID 陣列。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getUnprotectedRanges()
取得受保護工作表中未保護範圍的陣列。如果 Protection
物件
對應至受保護範圍而非受保護的工作表,此方法會傳回空白
陣列。如要變更未受保護的範圍,請使用 setUnprotectedRanges(ranges)
來設定
新的範圍陣列;如要重新保護整張工作表,請設定空陣列。
// Unprotect cells E2:F5 in addition to any other unprotected ranges in the protected sheet. var sheet = SpreadsheetApp.getActiveSheet(); var protection = sheet.protect(); var unprotected = protection.getUnprotectedRanges(); unprotected.push(sheet.getRange('E2:F5')); protection.setUnprotectedRanges(unprotected);
回攻員
Range[]
:受保護工作表中未受保護的範圍的陣列
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
isWarningOnly()
判斷保護區域是否使用「以警告為基礎」以及防護基於警告的保護措施
表示每位使用者都可以編輯該區域的資料,但在編輯提示時,
確認編輯內容。根據預設,受保護的範圍或工作表不會以警告為基準。目的地:
如要變更為警告狀態,請使用 setWarningOnly(warningOnly)
。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit') // Gets a sheet by its name. const sheet = ss.getSheetByName('Sheet1'); // Protects the sheet. const sampleProtectedSheet = sheet.protect(); // Sets the warning status for the protected sheet as true. sampleProtectedSheet.setWarningOnly(true); const protectedSheetWarningStatus = sampleProtectedSheet.isWarningOnly(); // Logs the warning status of the protected sheet to the console. console.log(protectedSheetWarningStatus);
回攻員
Boolean
:如果受保護的範圍或工作表僅使用警告依據保護,則為 true
。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
remove()
取消保護指定範圍或工作表。
// Remove all range protections in the spreadsheet that the user has permission to edit. var ss = SpreadsheetApp.getActive(); var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE); for (var i = 0; i < protections.length; i++) { var protection = protections[i]; if (protection.canEdit()) { protection.remove(); } }
// Remove sheet protection from the active sheet, if the user has permission to edit it. var sheet = SpreadsheetApp.getActiveSheet(); var protection = sheet.getProtections(SpreadsheetApp.ProtectionType.SHEET)[0]; if (protection && protection.canEdit()) { protection.remove(); }
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
removeEditor(emailAddress)
將特定使用者從受保護工作表或範圍的編輯者清單中移除。請注意,如果 使用者是擁有編輯權限的 Google 群組成員,或網域中的所有使用者 使用者仍可編輯受保護的區域,兩者皆非 或目前使用者皆可移除。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Gets a sheet by its name. const sheet = ss.getSheetByName('Sheet1'); // Creates a variable for an email address. // TODO(developer): Replace the email address with a valid one. const TEST_EMAIL = 'baklavainthebalkans@gmail.com'; // Protects the sheet. const sampleProtectedSheet = sheet.protect(); // Adds an editor to the protected sheet using the email address variable. sampleProtectedSheet.addEditor(TEST_EMAIL); // Removes the editor from the protected sheet using the email address variable. sampleProtectedSheet.removeEditor(TEST_EMAIL); // Gets the editors of the protected sheet. const editors = sampleProtectedSheet.getEditors(); // Logs the editors' email addresses to the console. for (const editor of editors) { console.log(editor.getEmail()); }
參數
名稱 | 類型 | 說明 |
---|---|---|
emailAddress | String | 要移除的使用者電子郵件地址。 |
回攻員
Protection
:代表鏈結設定的物件。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
removeEditor(user)
將特定使用者從受保護工作表或範圍的編輯者清單中移除。請注意,如果 使用者是擁有編輯權限的 Google 群組成員,或網域中的所有使用者 但他們仍然可以編輯受保護的區域。無論 試算表擁有者或目前的使用者皆可移除。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Gets a sheet by its name. const sheet = ss.getSheetByName('Sheet1'); // Protects the sheet. const sampleProtectedSheet = sheet.protect(); // Removes the active user from the editors of the protected sheet. sampleProtectedSheet.removeEditor(Session.getActiveUser()); // Gets the editors of the protected sheet. const editors = sampleProtectedSheet.getEditors(); // Logs the editors' email addresses to the console. for (const editor of editors) { console.log(editor.getEmail()); }
參數
名稱 | 類型 | 說明 |
---|---|---|
user | User | 要移除的使用者表示法。 |
回攻員
Protection
:代表防護設定的物件,用於鏈結
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
removeEditors(emailAddresses)
從受保護工作表或範圍的編輯者清單中移除指定使用者陣列。 請注意,如果其中一位使用者是擁有編輯權限的 Google 群組成員,或所有 網域中的使用者仍具備編輯權限, 試算表的擁有者和目前使用者都無法移除。
// Protect the active sheet, then remove all other users from the list of editors. var sheet = SpreadsheetApp.getActiveSheet(); var protection = sheet.protect().setDescription('Sample protected sheet'); // Ensure the current user is an editor before removing others. Otherwise, if the user's edit // permission comes from a group, the script throws an exception upon removing the group. var me = Session.getEffectiveUser(); protection.addEditor(me); protection.removeEditors(protection.getEditors()); if (protection.canDomainEdit()) { protection.setDomainEdit(false); }
參數
名稱 | 類型 | 說明 |
---|---|---|
emailAddresses | String[] | 要移除的使用者電子郵件地址陣列。 |
回攻員
Protection
:代表防護設定的物件,用於鏈結
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
removeTargetAudience(audienceId)
移除做為受保護範圍編輯者角色的指定目標對象。
參數
名稱 | 類型 | 說明 |
---|---|---|
audienceId | String | 要移除的目標對象 ID。 |
回攻員
Protection
:代表鏈結設定的物件。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setDescription(description)
設定保護範圍或工作表的說明。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Gets the sheet 'Sheet1' by its name. const sheet = ss.getSheetByName('Sheet1'); // Protects the sheet. const sampleProtectedSheet = sheet.protect(); // Sets the sheet description to 'Sheet1 is protected.' sampleProtectedSheet.setDescription('Sheet1 is protected'); // Gets the description of the protected sheet. const sampleProtectedSheetDescription = sampleProtectedSheet.getDescription(); // Logs the description of the protected sheet to the console. console.log(sampleProtectedSheetDescription);
參數
名稱 | 類型 | 說明 |
---|---|---|
description | String | 受保護範圍或工作表的說明。 |
回攻員
Protection
:代表鏈結設定的物件。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setDomainEdit(editable)
設定網域內所有使用者是否擁有試算表編輯權限 受保護的範圍或工作表。請注意,任何擁有明確編輯權限的使用者可以 編輯受保護的區域。如果試算表 不屬於 Google Workspace 網域 (也就是說,該網域的擁有者為 gmail.com 帳戶)。
參數
名稱 | 類型 | 說明 |
---|---|---|
editable | Boolean | 如果擁有試算表的網域中的所有使用者都應有 true
編輯受保護範圍或工作表的權限;false 表示不滿意。 |
回攻員
Protection
:代表防護設定的物件,用於鏈結
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setNamedRange(namedRange)
將保護範圍與現有的已命名範圍建立關聯。如果已命名範圍涵蓋
與目前保護範圍不同的範圍,這個方法會將防護效果移至
已命名範圍已命名範圍必須與目前保護的工作表相同
範圍。請注意,指令碼必須明確呼叫此方法,才能將受保護的範圍與
已命名範圍呼叫 Range.protect()
即可從 Range
建立保護
自己是已命名範圍,如果沒有呼叫 setRangeName(rangeName)
,則不會
才能產生關聯不過,如要透過
Google 試算表 UI 會自動為兩者建立關聯。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Protects cells A1:D10 on Sheet1. const sheet = ss.getSheetByName('Sheet1'); const protectedRange = sheet.getRange('A1:D10').protect(); // Logs the current protected range, A1:D10. console.log(protectedRange.getRange().getA1Notation()); // Creates a named range for cells E1:J10 called 'NewRange.' const newRange = sheet.getRange('E1:J10'); ss.setNamedRange('NewRange', newRange); const namedRange = ss.getNamedRanges()[0]; // Updates the protected range to the named range, 'NewRange.' // This updates the protected range on Sheet1 from A1:D10 to E1:J10. protectedRange.setNamedRange(namedRange); // Logs the updated protected range to the console. console.log(protectedRange.getRange().getA1Notation());
參數
名稱 | 類型 | 說明 |
---|---|---|
namedRange | NamedRange | 要與保護範圍建立關聯的現有已命名範圍。 |
回攻員
Protection
:代表鏈結設定的物件。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setRange(range)
調整要保護的範圍。如果指定範圍涵蓋的區域與 目前的保護範圍,這種方式會改為將保護措施移動到涵蓋新的範圍。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Protects cells A1:D10 on Sheet1 of the spreadsheet. const sheet = ss.getSheetByName('Sheet1'); const protectedRange = sheet.getRange('A1:D10').protect(); // Logs the original protected range, A1:D10, to the console. console.log(protectedRange.getRange().getA1Notation()); // Gets the range E1:J10. const newRange = sheet.getRange('E1:J10'); // Updates the protected range to E1:J10. protectedRange.setRange(newRange); // Logs the updated protected range to the console. console.log(protectedRange.getRange().getA1Notation());
參數
名稱 | 類型 | 說明 |
---|---|---|
range | Range | 新的範圍,以防編輯。 |
回攻員
Protection
:代表鏈結設定的物件。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setRangeName(rangeName)
將保護範圍與現有的已命名範圍建立關聯。如果已命名範圍涵蓋
與目前保護範圍不同的範圍,這個方法會將防護效果移至
已命名範圍已命名範圍必須與目前保護的工作表相同
範圍。請注意,指令碼必須明確呼叫此方法,才能將受保護的範圍與
已命名範圍呼叫 Range.protect()
即可從 Range
建立保護
自己是已命名範圍,如果沒有呼叫 setRangeName(rangeName)
,則不會
才能產生關聯不過,如要透過
Google 試算表 UI 會自動為兩者建立關聯。
// Protect a named range in a spreadsheet and log the name of the protected range. var ss = SpreadsheetApp.getActive(); var range = ss.getRange('A1:B10'); var protection = range.protect(); ss.setNamedRange('Test', range); // Create a named range. protection.setRangeName('Test'); // Associate the protection with the named range. Logger.log(protection.getRangeName()); // Verify the name of the protected range.
參數
名稱 | 類型 | 說明 |
---|---|---|
rangeName | String | 要保護的已命名範圍名稱。 |
回攻員
Protection
:代表防護設定的物件,用於鏈結
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setUnprotectedRanges(ranges)
取消保護受保護工作表中的指定範圍陣列。如果
Protection
物件會對應至受保護的範圍,而非受保護的工作表,或者
任何範圍均不在受保護的工作表中。如要變更未受保護的範圍,請設定新的
範圍陣列;如要重新保護整張工作表,請設定空陣列。
// Protect the active sheet except B2:C5, then remove all other users from the list of editors. var sheet = SpreadsheetApp.getActiveSheet(); var protection = sheet.protect().setDescription('Sample protected sheet'); var unprotected = sheet.getRange('B2:C5'); protection.setUnprotectedRanges([unprotected]); // Ensure the current user is an editor before removing others. Otherwise, if the user's edit // permission comes from a group, the script throws an exception upon removing the group. var me = Session.getEffectiveUser(); protection.addEditor(me); protection.removeEditors(protection.getEditors()); if (protection.canDomainEdit()) { protection.setDomainEdit(false); }
參數
名稱 | 類型 | 說明 |
---|---|---|
ranges | Range[] | 未受保護工作表中未受保護的範圍陣列。 |
回攻員
Protection
:代表防護設定的物件,用於鏈結
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setWarningOnly(warningOnly)
設定這個保護範圍是否使用「警告式」以及防護警告式
「保護」代表每位使用者都能編輯該區域中的資料,除了編輯提示外,
要求使用者確認編輯內容根據預設,系統不會設定受保護的範圍或工作表
警告製如要查看警告狀態,請使用 isWarningOnly()
。
// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/abc123456/edit'); // Gets the sheet 'Sheet1' by its name. const sheet = ss.getSheetByName('Sheet1'); // Protects the sheet and sets the protection to warning-based. const sampleProtectedSheet = sheet.protect().setWarningOnly(true); // Logs whether the protected sheet is warning-based to the console. console.log(sampleProtectedSheet.isWarningOnly());
參數
名稱 | 類型 | 說明 |
---|---|---|
warningOnly | Boolean |
回攻員
Protection
:代表鏈結設定的物件。
授權
使用這個方法的指令碼需要下列一或多個範圍的授權:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets