2

Right now I'm working on a project and I need to append a certain variable to an array. What I need to do is to read values from a spreadsheet and extract values that meet a certain criterion.

In mathematica I will do it like this

If[IFin="RECIBIDO", 
(*If true, do nothing*), 
(*else*) AppendTo[pendientes,{"project#", "IFin", IFin}]
]

But I need to apply it in a google sheets script, so right now I'm trying this (I'm using a switch case structure because of my needs)

var IFin=0 //it can take the following values 0, 1, 2, 3, "RECIBIDO"
var pendientes=[]; 
var InfOK = "RECIBIDO";

switch(IFin){
      case InfOk:
           break;
      case 0:
           [i,"IFin",IFin].appendTo(pendientes); 
           break;
      case 1:
           [i,"IFin",IFin].appendTo(pendientes); 
           break;
      case 2:
           [i,"IFin",IFin].appendTo(pendientes); 
           break;
      case 3:
           [i,"IFin",IFin].appendTo(pendientes); 
           break;
}

And all it says when I try to run it is "TypeError: Could not find funcion appendTo". I'm new to google scripts, and hence I'm using this https://www.w3schools.com/jquery/html_appendto.asp I've searched the google help and it has an extensive explanation of sheets functions.


Solution

This made me understand the root of the problem but proposed solution hasn't been tested yet:

appendTo isn't an array method in javascript. You could do something like pendientes = pendientes.concat([i,"IFin",IFIN])

This helped me solve the problem:

Try using push(). This method adds one or more elements to the end of an array and returns the new length of the array.

So by replacing

[i,"IFin",IFin].appendTo(pendientes);

by

pendientes.push([i,"IFin",IFin]);

my code worked just fine.

4
  • 1
    appendTo isn't an array method in javascript. You could do something like pendientes = pendientes.concat([i,"IFin",IFIN]) Commented May 5, 2017 at 21:36
  • Welcome. To ask question like "why this code isn't working", askers should include a minimal reproducible example. Please follow tour and checkout How to Ask. Commented May 6, 2017 at 18:33
  • Try using push(). This method adds one or more elements to the end of an array and returns the new length of the array. As also suggested in this SO post, you can first create a new array then append the values as arrays with one value. Commented May 7, 2017 at 10:28
  • Hi everyone! Thanks for helping! Using the push method worked just fine! Commented May 8, 2017 at 14:11

0

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.