5

Thanks in advance for your time here. I am self taught and new to coding, hence apologies if the question below is something an average programmer should already know.

I have created multiple functions in a single gas file, which when run individually calculate various aspects of same expenses data. But I need all functions to run in order to reach my end goal, which unfortunately I am unable to achieve. Please see examples below.

  1. Both functions when Run individually work without issues.

    function totalSpend(){
    var ss = SpreadsheetApp.getActiveSheet();
    var rt = ss.getRange("K2").getValue();
    var pt = ss.getRange("K1").getValue();
    var ts =  rt + pt ;
    ss.getRange("K3").setValue(ts); }
    
    function perHead(){
    var ss = SpreadsheetApp.getActiveSheet();
    var ts = ss.getRange("K3").getValues();
    var ph = ts / 2;
    ss.getRange("K4").setValue(ph);}
    
  2. But if I combine them both under a single function and then run, nothing happens. I tried using debugger and Execution Transcript but didn't have any luck. Below is example which doesn't work.

    function expCalc(){
    
    function totalSpend(){
    var ss = SpreadsheetApp.getActiveSheet();
    var rt = ss.getRange("K2").getValue();
    var pt = ss.getRange("K1").getValue();
    var ts =  rt + pt ;
    ss.getRange("K3").setValue(ts); };
    
    function perHead(){
    var ss = SpreadsheetApp.getActiveSheet();
    var ts = ss.getRange("K3").getValues();
    var ph = ts / 2;
    ss.getRange("K4").setValue(ph);};
    
    }
    
0

1 Answer 1

14
function expCalc(){
  totalSpend();
  perHead();
}

function totalSpend(){
var ss = SpreadsheetApp.getActiveSheet();
var rt = ss.getRange("K2").getValue();
var pt = ss.getRange("K1").getValue();
var ts =  rt + pt ;
ss.getRange("K3").setValue(ts); }

function perHead(){
var ss = SpreadsheetApp.getActiveSheet();
var ts = ss.getRange("K3").getValues();
var ph = ts / 2;
ss.getRange("K4").setValue(ph);}

or

function expCalc(){

var ss = SpreadsheetApp.getActiveSheet();
var rt = ss.getRange("K2").getValue();
var pt = ss.getRange("K1").getValue();
var ts =  rt + pt ;
ss.getRange("K3").setValue(ts);

var ts = ss.getRange("K3").getValues();
var ph = ts / 2;
ss.getRange("K4").setValue(ph);
}
Sign up to request clarification or add additional context in comments.

Comments

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.