1
votes

I have a "template" Google spreadsheet that I copy for each user when requested. The "template" spreadsheet has a Google Script associated with it but the link to this script appears to be lost when I do the copy.

The actual error I have is that there is a drawing in one of the sheets that is linked to a script and this link is lost when I copy the spreadsheet.

How do I copy the spreadsheet and copy the script so that they are linked in the new spreadsheet?

var masterSS = DriveApp.getFileById('999999999999999999999999999999');
var nowDT = new Date();
var newSS = 'ACME ' + nowDT;
masterSS.makeCopy(newSS);
1
Do you mean the script is bound to the spreadsheet you are copying? As in the script is in tools -> script editor of the spreadsheet? - James D
What do you mean by the link to this script appears to be lost? - Riyafa Abdul Hameed
The script is bound to the original 'template' spreadsheet (tools -> script editor of the spreadsheet) that I am copying with makecopy(). When I use tools -> script editor of the new spreadsheet I get the same code (different file ID). My problem is the drawing on the new spreadsheet sheet does not automatically link to the script in the new spreadsheet (as it was linked in the Template spreadsheet). - G Metcalf
can you post the script code where linking with spreadsheet is done - Ritesh Nair

1 Answers

1
votes

Thank you to James and Riyafa for helping me clarify exactly what my problem was. I have now added a script that is triggered as soon as the Spreadsheet is opened (I put it on the Template Spreadsheet and it is copied along with everything else to the new Spreadsheet). This script creates a Custom Menu that allows me to call other code in the script when needed (in my case function "Main"). It appears that putting the menu creation as script then links to the correct other script function in the new spreadsheet.

function onOpen() {
   var ui = SpreadsheetApp.getUi();
   ui.createMenu('Custom Menu')
  .addItem('Run Calculator', 'Main')
  .addToUi();

}