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!

1 Answer 1

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

3 Comments

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.
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: stackoverflow.com/a/44212505/3555636 and you could probably take a course on JS on codecademy to learn more about the stuff: stackoverflow.com/a/44212505/3555636
pasted the same link twice, the codecademy one should have been: codecademy.com/catalog/language/javascript — but you probably have found it already

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.