Home > Net >  Disable Spreadsheet Copy
Disable Spreadsheet Copy

Time:03-28

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

CodePudding user response:

The code doesn't disable spreadsheet copy, what it does is that, if the spreadsheet id is not equal to certain value it to iterates over all the sheets but it's not possible to delete all the sheets as each spreadsheet should include at least one sheet. You might change the script i.e. inserting a new sheet and delete the other sheets, but the editor will be able to restore the deleted data from the version history.

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.

Related

CodePudding user response:

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.

  • Related