I believe your goal as follows.
- When the script of
Protect()
is run by an user who is not the owner, you want to permit to copy the sheet and don't want to add the user as the editor to the copied whole sheet using Google Apps Script.
- The Spreadsheet has already been shared with the users.
Modification points:
- In this case, I thought that to run the script by the owner might be the solution of your issue, when the user runs the script.
- When the user is run the script, in order to run the script by the owner who is not the user, I would like to propose to use Web Apps.
Usage:
1. Prepare script.
Please copy and paste the following script to the script editor and save it.
function doGet() {
script();
return ContentService.createTextOutput();
}
function Protect() {
const url = ScriptApp.getService().getUrl();
UrlFetchApp.fetch(url, {headers: {authorization: "Bearer " + ScriptApp.getOAuthToken()}});
// DriveApp.getFiles() // This is used for automatically detecting the scope of "https://www.googleapis.com/auth/drive.readonly". This scope is used for the access token.
}
// This script is the same with your "Protect".
function script() {
var spreadsheet = SpreadsheetApp.getActive();
var myValue = SpreadsheetApp.getActiveSheet().getSheetName();
spreadsheet.duplicateActiveSheet();
var totalSheets = countSheets(); //script function
myValue = "DO" + totalSheets;
SpreadsheetApp.getActiveSpreadsheet().renameActiveSheet(myValue);
var protection = spreadsheet.getActiveSheet().protect();
protection.setUnprotectedRanges([spreadsheet.getRange('C2:E5'), spreadsheet.getRange('C6:D7'), spreadsheet.getRange('F5:G6'), spreadsheet.getRange('B9:G18'), spreadsheet.getRange('G7:G8')])
.removeEditors(['user1.com', 'user2.com', 'user3.com']);
spreadsheet.getRange('G2').setValue(myValue);
spreadsheet.getRange('G3').setValue(new Date()).setNumberFormat('dd-MMM-YYYY');
spreadsheet.getRange('H1:H').clearContent();
};
- In this script,
countSheets()
is not included. Because I'm not sure about countSheets()
from your question. So please be careful this. Please prepare this.
2. Deploy Web Apps.
- On the script editor, Open a dialog box by "Publish" -> "Deploy as web app".
- Select "Me" for "Execute the app as:".
- By this, the script is run as the owner.
- Select "Anyone" for "Who has access to the app:".
- In this case, the access token is required to request to Web Apps.
- Click "Deploy" button as new "Project version".
- Automatically open a dialog box of "Authorization required".
- Click "Review Permissions".
- Select own account.
- Click "Advanced" at "This app isn't verified".
- Click "Go to ### project name ###(unsafe)"
- Click "Allow" button.
- Click "OK".
3. Test this workaround.
Please click the button assigned with Protect
. By this, the script is run by the owner. By this, even when the user is clicked the button, the result of script is the same with that run by the owner.
Note:
- Please use this script with enabling V8.
References:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…