Mishal
Mishal

Reputation: 430

Adding Array from Variable on Google Sheets - Google Apps Script

I have an existing code which looks like,

var copyPageNumbers = [5, 7, 9];

I have the same number 5, 7, 9 in a column on my google sheet and have set up a variable to get value,

var credentials = ss.getRange(i, 11).getValue(); // The value of this is 5, 7, 9

Based on the variables above. I want to use a code which uses the credentials variable in the copyPageNumbers variable to look something like this,

var copyPageNumbers = [credentials];

For example, the below chunk of code works,

  var credentials = ss.getRange(i, 11).getValue();
  var copyPageNumbers = [2, 4];

  var offset = 1;

  var slides = otherPresentation.getSlides();
  var page = 0;
  slides.forEach(function(slide, i) {
    if (copyPageNumbers.indexOf(i + 1) != -1) {
      currentPresentation.insertSlide(offset + page, slide);
      page++;
    }
  });

However when I try both the methods stated below - it doesnt work,

  var credentials = ss.getRange(i, 11).getValue().split(", ");
  var copyPageNumbers = credentials
  var offset = 1;
  var slides = otherPresentation.getSlides();
  var page = 0;
  slides.forEach(function(slide, i) {
    if (copyPageNumbers.indexOf(i + 1) != -1) {
      currentPresentation.insertSlide(offset + page, slide);
      page++;
    }
  });
  var credentials = ss.getRange(i, 11).getValue();
  var offset = 1;
  var copyPageNumbers = []
  copyPageNumbers.push(credentials);
  var slides = otherPresentation.getSlides();
  var page = 0;
  slides.forEach(function(slide, i) {
    if (copyPageNumbers.indexOf(i + 1) != -1) {
      currentPresentation.insertSlide(offset + page, slide);
      page++;
    }
  });

Upvotes: 0

Views: 1118

Answers (3)

Mishal
Mishal

Reputation: 430

Thanks to the above answers and thanks to Pattern 2 here, I have managed to solve this.

See updated script below. The reason it was not working before was because of the For each loop which was looping the slide and the number and the array was not working.

  var credentials = ss.getRange(i, 11).getValue().split(", ");

  var offset = 0;

  var slides_stack = otherPresentation.getSlides();
  var page = 0;

    credentials.forEach(function(p, i) {
    currentPresentation.insertSlide(offset + i, slides_stack[p - 1]);
  });

Upvotes: 0

timmer
timmer

Reputation: 159

var copyPageNumbers = [];
var credentials = ss.getRange(i, 11).getValue();
copyPageNumbers.push(credentials);

You want an array ([5,7,9]), 'credentials' is a string (5,7,9). With the (javascript) push() method that string is added to the array 'copyPageNumbers'. If I understand it well.

Upvotes: 2

Diego
Diego

Reputation: 9571

var credentials = ss.getRange(i, 11).getValue(); return the string "5, 7, 9", but you need that as an array.

Assuming that the format is consistent (i.e., [number][comma][space]), you can use split() to turn it into an array.

var credentials = ss.getRange(i, 11).getValue().split(", "); // [5, 7, 9]
var copyPageNumbers = credentials;

Upvotes: 2

Related Questions