Search code examples
google-apps-scriptgoogle-sheetsgoogle-sheets-macros

Having problems with Multiple Google Apps Scripts on one Sheet


I have created two separate scripts to perform two separate actions on the same sheet, and they should be run independently but I cannot seem to figure out how to link them both to the same spreadsheet as only one appears in the macro list.

I have tried to create both scripts via the script editor link from the sheet and both appear in the developer hub, but I just cannot seem to have both shown in the macro dropdown selection.

This is one script to copy the contents of the source sheet to the target sheet, which is created as part of this script and it works fine on it's own.

var ss = SpreadsheetApp.getActive();
var sheetName = ss.getSheetByName("Results");
var CopyNew = function () {  
var nm = ss.getSheetByName("NEW");
if(!nm){
  var newSheet = ss.insertSheet("NEW");
  var sourceRange = sheetName.getRange("A1:N100");
  var targetRange = newSheet.getRange("A1:N100");
  targetRange.setValues(sourceRange.getValues());
  sourceRange.copyTo(targetRange, {formatOnly:true});
    }
}
CopyNew();

This is a separate script which should be performed after the first script on the source sheet to clear certain columns and again this works fine on it's own.

function ClearCells() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('G9:G100').activate();
spreadsheet.getActiveRangeList().clear({contentsOnly: true, 
skipFilteredRows: true});
spreadsheet.getRange('K9:K100').activate();
spreadsheet.getActiveRangeList().clear({contentsOnly: true, 
skipFilteredRows: true});
spreadsheet.getRange('N9:N100').activate();
spreadsheet.getActiveRangeList().clear({contentsOnly: true, 
skipFilteredRows: true});
};

I would like to have both these scripts available in the macro dropdown so I can run them independently but only one is shown.


Solution

  • @ziganotschka Thank you for your reply, I figured this out with your response. It was a problem with the script itself. Because you mentioned that I'm not importing the script but a function, it made me look at the function part, which was written in a way that was confusing Google I guess! I had written it like this:

    var CopyNew = function () {  
    

    But I have now changed it to this below and it resolved the problem.

    function CopyNew() {  
    

    Thank you, everyone, for your help and suggestions, everything is working perfectly now.