0

First, I'd like to apologize in advance because I think (hope?) this is an easy answer but I've been searching and testing without a successful result.

I have a column of data in a Google Sheet, with each cell value containing an independent value:

Column A
abc
def
etc.

I have function that sends this data to another place for crunching, but first I need to add quotation marks around each cell value like so:

Column A
"abc"
"123"
"etc."

Currently I'm doing this via a concatenate in a helper column, but I'd like to do this directly within the Script. Right now all my attempts have resulted in this output in the logger:

"abc, 123, etc."

Rather than the intended result of "abc","123","etc.".

Thanks in advance!

deL
  • 91
  • 1
  • 1
  • 7

1 Answers1

1
value = '"' + value + '"';

should do the trick.

Or, if you're getting your values via getValues function:

  var values = ss.getRange('A1:A3').getValues();
  for (var i = 0; i < values.length; i++) {
    for (var j = 0; j < values[0].length; j++) {
      values[i][j] = '"' + values[i][j] + '"'; 
    }
  }
  Logger.log(values);
  ss.getRange('A1:A3').setValues(values);
a-change
  • 656
  • 5
  • 12
  • Thanks a-change I am indeed using getValues and this works! One favor to ask though: Can you explain or point me to abstract documentation that explains the underlying reason why this works? Basically, I see you're declaring two new variables (I and J) and running a loop, but I don't really follow the mechanics behind it. – deL Nov 01 '17 at 21:42
  • 1
    That is looping through an array (which is a variable that contains many values or other variables at once). `getValues` returns a two-dimensional array meaning that each value of the first array is also an array which can have lots of different values. The first loop iterates over the array of arrays (rows of your table), the second — over the 'array of values' (columns). See more here: https://stackoverflow.com/a/44212505/3555636 and you could probably take a course on JS on codecademy to learn more about the stuff: https://stackoverflow.com/a/44212505/3555636 – a-change Nov 01 '17 at 21:59
  • pasted the same link twice, the codecademy one should have been: https://www.codecademy.com/catalog/language/javascript — but you probably have found it already – a-change Nov 02 '17 at 10:33