存取及修改受保護的範圍和工作表。受保護的範圍可保護靜態儲存格範圍或已命名範圍。受保護的工作表可能包含未受保護的區域。如果是使用舊版 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)
不過,在 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