-1

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

Rubén
  • 34,714
  • 9
  • 70
  • 166
Pho
  • 3
  • 3
  • 2
    This feels like you're trying to solve the wrong problem: if you put data on a publicly accessible webpage, then that is publicly accessible data: add a clear license indicator that informs your visitors that copying the data you're showing is not permitted. Because as long as the browser renders your data, people can copy it. No matter how much JS you use, the fact that the browser has to download the data in order to show it at all means that anyone can copy it by using the dev tools network tab and saving the sheet's network response, completely outside the control of your page. – Mike 'Pomax' Kamermans Mar 27 '22 at 16:25

2 Answers2

1

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

Rubén
  • 34,714
  • 9
  • 70
  • 166
0

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.

Mike Steelson
  • 14,650
  • 2
  • 5
  • 20