0

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++;
    }
  });

3 Answers 3

2

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;
Sign up to request clarification or add additional context in comments.

3 Comments

thanks @Diego however this method is not working with my script. Please see edits made to question.
@MohammedMishal What are the outputs of Logger.log(ss.getRange(i, 11).getValue()) and Logger.log(ss.getRange(i, 11).getValue().split(", "))?
here are the outputs [20-02-14 20:08:37:151 GST] 3, 4 and [20-02-14 20:08:37:248 GST] [3, 4] even though it output look like they are an array the script to include slides 3 and 4 don't seem to work compared to when I hard key the numbers 3 and 4 into the script.
2
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.

7 Comments

The OP(?) wants (I think) an array, 'credentials' is a string, not an array. With the (javascript) push() method the string is added to the array 'copyPageNumbers'. If I understand it well.
thanks @timmer however this method is not working with my script. Please see edits made to question.
@MohammedMishal (1) In 'my' answer var credentials = ss.getRange(i, 11).getValue(); (2) Should '!=' in the if thing not be '!==' ?
@TimmerI have done that and it still doesn't work compared to when I hard code the numbers in the array. would be grateful if you could share any alternative ways
@MohammedMishal >> Perhaps it does not work because 'offset = 1' is missing in 'our' answers
|
0

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]);
  });

Comments

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.