1

I'm very new to writing code, and I need some help. I'm working on google sheets. In the code below, I was trying to call the Merge() function within the copyAllInRange() function. I saw many examples, but I'm a bit confused about how to do it. Both functions work well; I don't know how to call one function into another.

The objective is to run copyAllInRange(); and at the end of the function call the Merge() function to run.


 function Merge(){
     var ss = SpreadsheetApp.getActiveSpreadsheet();
     var sheet = ss.getSheetByName('PDF_TEMP');
     var Range_Logo = sheet.getRange('C11:D16').merge();
     var Range_Bottom = sheet.getRange('C50:J52').merge();
 
 Range_Bottom.setHorizontalAlignment("center").setVerticalAlignment("middle");
 Range_Logo.setHorizontalAlignment("center").setVerticalAlignment("middle");
 }

 function copyAllInRange(){
 var SS = SpreadsheetApp.getActiveSpreadsheet(); 
 var NS = SS.getSheetByName("PDF_TEMP");    
     if (NS != null) {
         SS.deleteSheet(NS);}
     NS = SS.insertSheet();
     NS.setName("PDF_TEMP");
 
 var Sheet = SS.getSheetByName("Factura");     
 var TR = Sheet.getRange("C3:N59");        
 var PR = NS.getRange("A1:L55");               
   
 var columnWidths = SpreadsheetApp.CopyPasteType.PASTE_COLUMN_WIDTHS
   TR.copyTo(PR);
   TR.copyTo(PR,columnWidths,false);  
 }

Both are in the same script file; both work independently.

2 Answers 2

1

You can call the second function at the end of the merge function

function Merge(){
     var ss = SpreadsheetApp.getActiveSpreadsheet();
     var sheet = ss.getSheetByName('PDF_TEMP');
     var Range_Logo = sheet.getRange('C11:D16').merge();
     var Range_Bottom = sheet.getRange('C50:J52').merge();
 
 Range_Bottom.setHorizontalAlignment("center").setVerticalAlignment("middle");
 Range_Logo.setHorizontalAlignment("center").setVerticalAlignment("middle");
 
 copyAllInRange()
 
 }

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer. I tried, but still not working. First, it has to copy all the data and then merge the cells.
0

Note:

Keep in mind to share a sample sheet/screenshot of what you're working on when possible so that that we will be able to visualize better your goal and this will also help us provide you a better answer/recommendation.

Recommendation:

I've tested your code and created a sample sheets based on your code. You may use this code below as reference:

 function copyAllInRange(){// Please select this copyAllInRange function before running the script
    var ss2 = SpreadsheetApp.getActiveSpreadsheet(); 
    var ns = ss2.getSheetByName("PDF_TEMP");    
        if (ns != null) {
            ss2.deleteSheet(ns);}
        ns = ss2.insertSheet();
        ns.setName("PDF_TEMP");
    
    var sheet = ss2.getSheetByName("Factura");     
    var tr = sheet.getRange("C3:N59");        
    var pr = ns.getRange("A1:L55");               
      
    var columnWidths = SpreadsheetApp.CopyPasteType.PASTE_COLUMN_WIDTHS;
      tr.copyTo(pr);
      tr.copyTo(pr,columnWidths,false);  
      Logger.log("First Run: Done running copyAllInRange()"); //Added a log line so that you can track how the code runs in Execution logs on the Apps Script editor
      merge();//last function to be run before the code ends
 }

  function merge(){
     var ss = SpreadsheetApp.getActiveSpreadsheet();
     var sheet = ss.getSheetByName('PDF_TEMP');
     var range_Logo = sheet.getRange('C11:D16').merge();
     var range_Bottom = sheet.getRange('C50:J52').merge();
 
     range_Bottom.setHorizontalAlignment("center").setVerticalAlignment("middle");
     range_Logo.setHorizontalAlignment("center").setVerticalAlignment("middle");
     Logger.log("Second Run: Done running merge()"); //Added a log line so that you can track how the code runs in Execution logs on the Apps Script editor
 }

I've corrected some your code above, as you've been using an in correct naming convention (PascalCase) on some of your variable names such as var SS = renamed to (CamelCase) var ss =. This is because when naming variables, arrays, or other elements, always start in lowercase letter or in "CamelCase". Using capitalized letters or "PascalCase" is used for naming functions, classes, and other objects and this could problems in your code when you use it in naming variables

Here's my sample PDF_TEMP Sheet:

enter image description here

Here's my sample Factura Sheet:

enter image description here

After running the code, here's the result on the PDF_TEMP sheet:

enter image description here

Here's the execution log result:

enter image description here

2 Comments

Thank you for trying, correcting the code, and for your guidance; now there's no problem when running it. Everything works perfectly!
You're very welcome :). You may also accept this answer so that others who are also looking for the same question will be able to see this post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.