I have a Google Sheet template with a button that triggers a macro to make a new copy of itself that can than be edited (code below). I'd like the macro to remove the button on its copy, as there's no need to make a copy of the copy. However, when I tried to record the steps to delete the button, the code doesn't appear in the resulting macro.
Is there code that can be added to my CopySheet macro that remove the button object?
Any help would be appreciated.
function CopyDA() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('A1').activate();
spreadsheet.copy('Deal Analyser - ');
};
Best Answer
You want to copy a spreadsheet template by clicking a button, and then delete the button in the newly copied spreadsheet.
You are using a macro to copy the spreadsheet. A macro is a Google Apps Script that is automatically created by user UI interactions. Macros have their strengths and weaknesses but in this case, a macro can't manage the steps required to delete the button on the new spreadsheet. You will need to substitute the macro for a script like the one below.
Logic
var newsheet = ss.copy('Deal Analyser - ').getId();
- a variable is assigned to the result andgetid()
gets the unique identifier for the newly creatred spreadsheetvar newtemplate = SpreadsheetApp.openById(newsheet);
- opens the newly created spreadsheet for editing by usingopenById(id)
var newtemplatesheet = newtemplate.getSheets()[0];
- gets the first (and only) sheet from the newly created spreadsheet.var drawing = newtemplatesheet.getDrawings();
- gets all the drawings on the first sheet.drawing[0].remove();
- deletes the first drawing from the spreadsheet.References
getId()
openById().
getSheets()
getDrawings()
remove()