最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Passing variables between functions by reference in Google Apps Script - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question asked Jan 27, 2020 at 4:29 maxgotstuckmaxgotstuck 1591 silver badge12 bronze badges 1
  • 3 A primitive is always passed by value in js, and an object always by reference. So, you can rig a simple solution by passing a config object between the functions, and using one of its properties to share state. – tehhowch Commented Jan 27, 2020 at 4:58
Add a ment  | 

2 Answers 2

Reset to default 5
  • 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.

tehhowch's answer is correct. Pass by value and pass by reference are described in the 3rd paragraph of this section of the javascript documentation.

"The parameters of a function call are the function's arguments. Arguments may be passed by value (in the case of primitive values) or by reference (in the case of objects). This means that if a function reassigns a primitive type parameter, the value won't change outside the function. In the case of an object type parameter, if its properties are mutated, the change will impact outside of the function."

发布评论

评论列表(0)

  1. 暂无评论