简体   繁体   中英

Call a function within another function in the same script in Google Apps Script

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.

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() }

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:

在此处输入图像描述

Here's my sample Factura Sheet:

在此处输入图像描述

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

在此处输入图像描述

Here's the execution log result:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM