Can someone help me fix the following code?
I using this script to disable editor to copy my Google spreadsheet
function onOpen () {
const ss = SpreadsheetApp.getActive();
const id = ss.getId();
if (id !== '1ld8aPE5zVBYoT39WPzZjWxU1uHJ8lVVV5vkBiv5USzI') ss.getSheets().forEach((s) => ss.deleteSheet(s)); }
Reference.
Disable Spreadsheet copy - Google Sheets
I tried copying to another worksheet but code wont work
Example https://drive.google.com/drive/folders/11Jqh_xaw0CdI1cBUL_hZwCjOJNnRKSU6?usp=sharing
Tl;Dr There is no way to prevent that spreadsheet editors copy the spreadsheet, because of this, in order to prevent that editors copy the spreadsheet you should use another approach to whatever you are doing with your spreadsheet.
Regarding the code in the question, it doesn't disable spreadsheet copy, what it does is, if the spreadsheet id is not equal to certain value, iterates over all the sheets but as it's not possible to delete all the sheets as each spreadsheet should include at least one sheet, the script throws an error. You might change the script i.e. inserting a new sheet and delete the other sheets, but the editor of the original spreadsheet, as there are the owner of the copy, they will be able to delete the script and restore the deleted data from the version history of the new spreadsheet.
Related
At least, you can add a sheet with an alert and delete all other sheets
var id = '1ld8aPE5zVBYoT39WPzZjWxU1uHJ8lVVV5vkBiv5USzI'
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
if (ss.getId() != id) {
var gid = ss.insertSheet().getSheetId()
ss.getSheets().forEach(function (sh) {
if (sh.getSheetId() != gid) { ss.deleteSheet(sh) }
})
}
}
I am not sure that there is no bypass! See Rubén's advice.