javascript - Array .Push not adding to end of Array - Stack Overflow

When editing a cell and logging the output of the Array, PUSH function has not added the newly edited c

When editing a cell and logging the output of the Array, PUSH function has not added the newly edited cell to the end of the array; it only outputs one element, almost as though it's recreating the array each time you edit a cell.

var EnteredDates = [];

function OnEdit(e){
var range = e.range;

EnteredDates.push([range.getValue(),range.getA1Notation()]);

}

When editing a cell and logging the output of the Array, PUSH function has not added the newly edited cell to the end of the array; it only outputs one element, almost as though it's recreating the array each time you edit a cell.

var EnteredDates = [];

function OnEdit(e){
var range = e.range;

EnteredDates.push([range.getValue(),range.getA1Notation()]);

}

I've tried searching the stack overflow questions. but can't see this exact problem of an array being re-created when pushing.

I've also tried initiating the Array as a Global variable and also, instead, initiated it inside OnOpen Function. Both don't work.

Additionally I've also removed initiating it as a Global Variable incase it is always being initiated on code being run and tried the following; when logging the EnteredDates array it still only shows the latest entry.

if (!Array.isArray(EnteredDates)) {
   var EnteredDates = [];
}
                
EnteredDates.push([range.getValue(),range.getA1Notation()]);
Logger.log(EnteredDates);

Share Improve this question edited Jul 24, 2019 at 14:57 Phil asked Jul 23, 2019 at 17:28 PhilPhil 451 silver badge6 bronze badges 5
  • where is code ? – Akhilesh Commented Jul 23, 2019 at 17:29
  • 3 Do you have code? My guess is that you are expecting push to return the new array but it doesn't. It only returns the length of the new array. The original array is modified. – ktilcu Commented Jul 23, 2019 at 17:30
  • initiated it inside OnOpen Function -> It seems like every time the function is called the array is set to empty array i:e arrayName = [], which will delete already inserted elements and re initiate to empty array. – random Commented Jul 23, 2019 at 17:39
  • Provide code if you want a precise answer. – Cooper Commented Jul 23, 2019 at 17:57
  • Sorry guys, I'm not sure what happened with the website; I thought i entered the code. PLease refresh you'll see it now. Very simple.. just a Push function in OnEdit – Phil Commented Jul 23, 2019 at 18:02
Add a ment  | 

2 Answers 2

Reset to default 3

an array being re-created when pushing

as already mentioned by @ktilcu, this is not the case with .push(), the original array is modified, no new array is created.

Lacking any code example in your question I assume you're doing something like this:

var cells = ['cell 1', 'cell 2', 'cell 3'];

console.log(cells.push('cell 4'));

the console logs 4, which is the length of the modified array, not the array itself or the item you just pushed to it, e.g. it's kind of the same as if you would do:

var cells = ['cell 1', 'cell 2', 'cell 3'];

cells.push('cell 4');

console.log(cells.length)

to actually "see" the modified array, you would have to do something like this:

var cells = ['cell 1', 'cell 2', 'cell 3'];

cells.push('cell 4');

console.log(cells);

I had similar problems getting global array to work. So I used the following instead.

function onEdit(e) {
  var sh=e.range.getSheet();
  if(sh.getName()!='Sheet177')return;
  pushArray(e.value + "," + e.range.getA1Notation());
  e.source.toast(e.value + ', ' + e.range.getA1Notation());
}

function pushArray(value) {
  PropertiesService.getScriptProperties().setProperty('array', PropertiesService.getScriptProperties().getProperty('array') + ',' + value)
}

function getArray() {
  return PropertiesService.getScriptProperties().getProperty('array');
}

function runOne() {
  SpreadsheetApp.getUi().alert(getArray());
}

function runTwo() {
  PropertiesService.getScriptProperties().setProperty('array', '');
}

runOne and runTwo are functions that are builtin to my menu so using those function names keeps me from having to add them to my menu all of the time.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745420597a4626948.html

相关推荐

  • javascript - Array .Push not adding to end of Array - Stack Overflow

    When editing a cell and logging the output of the Array, PUSH function has not added the newly edited c

    6小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信