5

I'm new here (second post) and also new in Google Apps Script.

I'm trying to find out how to write data to a single range in a spreadsheet using Google Apps Script.

Looks like Google Help in the section Writing data is not good enough

to explain to beginners, for example, how to write TEXT in cell B5.

7

You start by getting a reference to a Range with the getRange function. Once we have that, and since this range consists of a single cell, we can use the setValue function:

var range = SpreadsheetApp.getActiveSpreadsheet().getRange("B5");
range.setValue("TEXT");

... which will write TEXT to cell B5.

If you have a multi-cell range (which is more common), and you don't want to write the same value to all of the cells, you need to use the setValues function (note the plural), which takes an array of arrays ([[]]) as argument:

var range = SpreadsheetApp.getActiveSpreadsheet().getRange("B5:C5");
range.setValues([ ["This is column B", "This is column C"] ]);

In this case, we have two cells on the same row, so the outer array has a single element: the inner array, which again have two elements - one for each cell.

Compare to the next example, where we want to write to a range that spans multiple rows:

var range = SpreadsheetApp.getActiveSpreadsheet().getRange("B5:B7");
range.setValues([ ["This is row 5"], ["This is row 6"], ["This is row 7"] ]);

And finally, let's write to a multi-column, multi-row range:

var range = SpreadsheetApp.getActiveSpreadsheet().getRange("B5:C7");
range.setValues([ ["This is B5", "This is C5"], ["This is B6", "This is C6"], ["This is B7", "This is C7"] ]);

I have created an example spreadsheet to demonstrate these snippets - feel free to copy it.


Please note that any function that directly fetches or manipulates spreadsheet data directly, is slow, regardless of the amount of data involved. So keep the number of spreadsheet function calls low. If you want to update multiple cells, it is usually better to fetch the full array of values first, then update the values you want to change, and write back the whole array.

In this example, we have a spreadsheet with data from A1 to H8, but we want to update only two cells: D7 and G6. The rest of the cells should be unchanged.

var range = SpreadsheetApp.getActiveSpreadsheet().getRange("A1:H8");
var values = range.getValues();
values[6][3] = "This is D7";
values[5][6] = "This is G6";
range.setValues(values);

Note how we use range.getValues() to get an array of the existing values. Then we set the individual values in that array. Since JavaScript array indexes are 0-based, row 7 will be 6, and column D, being the 4th column, will be 3 - hence values[6][3]. Similarly, G6 will be values[5][6].

Finally, we write the array back to the range. We can update as many values we like in the array, while keeping the number of spreadsheet function calls low - only a single getValues and a single setValues.


From your comment it is probably worth mentioning how a pure function can be used from a spreadsheet. A pure function is a function that has no side effects, that is, its return value depends only on its arguments.

A pure function can be called from the spreadsheet with =functionName. Let's say you want a function multiplies an input value with 2, and returns its result. Enter this in the script editor:

function multiplyByTwo(num) {
   return num * 2;
}

In your spreadsheet, let's say in cell A2, you would enter the formula =multiplyByTwo(4). A2 will then display 8.
You could also use other cells as arguments. Let's change the formula to =multiplyByTwo(B2), and in cell B2 enter the number 6. Cell A2 will now display 12.
Here's another example for pure functions, and read more about Custom functions in Google's documentation.

You can also return a multi-dimensional array, in which case the result will span several rows/columns in the spreadsheet. The function

function multiplyByTwoAndFour(num) {
    return [[num * 2], [num * 4]];
}

can be used in cell D2 as =multiplyByTwoAndFour(3), which will cause D2 to display 6 and D3 to display 12.

  • More than complete. Thanks for supporting the beginner. Just one doubt: In the spreadsheet you provide, where do you call those functions? – Shirley Temple May 29 '17 at 16:13
  • "where do you call those functions?" I mean, I saw the inside code with your functions but I don't get from where you're calling them. – Shirley Temple May 29 '17 at 16:30
  • 1
    @SergioCruzCamposJiménez Good catch, I don't call these functions from the spreadsheet, I've just been running them from the script editor (the Play button). You could call any function from the spreadsheet with =functionName(), but then your function should return something, instead of manipulating data. – Vidar S. Ramdal May 29 '17 at 16:49
  • 1
    @SergioCruzCamposJiménez ... which, perhaps, is what you need in this case. I have appended my answer with a section about pure functions. – Vidar S. Ramdal May 29 '17 at 17:33
  • 1
    Thanks for italicizing "array of arrays". I've been banging my head trying to figure out why using setValues() on a range wasn't working for me for a single row, and this was the cause. – Sk606 Dec 20 '17 at 22:16
0

I created this small function which i find quite useful, it makes it easier to put data into ranges.

function sheetSetRange(data_array,range_addr) {
   data_range = SpreadsheetApp.getActiveSpreadsheet().getRange(range_addr)  
   data_range.setValues(data_array) 
}

when you have that in your .gs file, you can do like this :

to set the value of 13 cells from G21 to S21

sheetSetRange([["z","y","y","y","y","y","y","y","y","y","y","y","z"]],"G21:S21")

to set the value of 4 cells in B2,B3,C2,C3 to 1,2 3,4

sheetSetRange([[1,2][3,4]],"B2:C3")

You can probably think up a better name for the function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.