I’m really new to Google Apps Script and just started a few days ago to learn it. I have previous knowledge in C++ but not more.
I tried to write my first script doing some action on a google sheet. Here is the code I created so far:
var ss = SpreadsheetApp.getActiveSpreadsheet(); var s = ss.getActiveSheet(); function MainFunc() { var LoopCounter = 0; SaveDataInArray(LoopCounter); Logger.log(LoopCounter); } function SaveDataInArray(LoopCounter) { var InfiniteForLoopTrigger = 1; var range; var VolumeArray = []; var SignalArray = []; //Save Data in Array for(var i = 2; InfiniteForLoopTrigger > 0; i++){ range = s.getRange(i, 1); if(range.isBlank()){ break; } VolumeArray[LoopCounter] = s.getRange(i, 1).getValue(); SignalArray[LoopCounter] = s.getRange(i, 2).getValue(); LoopCounter++; } }
I realized that the value of LoopCounter is not changed, after being passed to the function SaveDataInArray. I know that having an ampersand (&) in front of the argument, like
function SaveDataInArray(& LoopCounter)
helps the problem in C++, but I don’t know how to get along with it in Google Apps Script.
Would love if someone could help me on that. Thanks and best regards, Max
Advertisement
Answer
- You want to call the value by the reference using Google Apps Script.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Issue and workaround:
Unfortunately, the variable of the number and string cannot be used as call by reference. For example, when the following script is run, 0.0, 0, 1.0, 1.0
can be seen at Logger.log
.
function work(a, b, c, d) { a = 1; b = "1"; c[0] = 1; d.value = 1; } function run() { var a = 0; var b = "0"; var c = [0]; var d = {value: 0}; work(a, b, c, d); Logger.log("%s, %s, %s, %s", a, b, c[0], d.value) // 0.0, 0, 1.0, 1.0 }
So, in this case, please use the object like below.
Pattern 1:
In this pattern, an array is used as the object.
function MainFunc() { var LoopCounter = [0]; SaveDataInArray(LoopCounter); Logger.log(LoopCounter[0]); } function SaveDataInArray(LoopCounter) { var ss = SpreadsheetApp.getActiveSpreadsheet(); var s = ss.getActiveSheet(); var InfiniteForLoopTrigger = 1; var range; var VolumeArray = []; var SignalArray = []; //Save Data in Array for(var i = 2; InfiniteForLoopTrigger > 0; i++){ range = s.getRange(i, 1); if(range.isBlank()){ break; } VolumeArray[LoopCounter[0]] = s.getRange(i, 1).getValue(); SignalArray[LoopCounter[0]] = s.getRange(i, 2).getValue(); LoopCounter[0]++; } }
Pattern 2:
In this pattern, a JSON object is used as the object.
function MainFunc() { var LoopCounter = {value: 0}; SaveDataInArray(LoopCounter); Logger.log(LoopCounter.value); } function SaveDataInArray(LoopCounter) { var ss = SpreadsheetApp.getActiveSpreadsheet(); var s = ss.getActiveSheet(); var InfiniteForLoopTrigger = 1; var range; var VolumeArray = []; var SignalArray = []; //Save Data in Array for(var i = 2; InfiniteForLoopTrigger > 0; i++){ range = s.getRange(i, 1); if(range.isBlank()){ break; } VolumeArray[LoopCounter.value] = s.getRange(i, 1).getValue(); SignalArray[LoopCounter.value] = s.getRange(i, 2).getValue(); LoopCounter.value++; } }
If I misunderstood your question and this was not the direction you want, I apologize.