28

and sorry for the stupid question.

I know how to program on VBA for Excel, but I´m having a hard time doing the simplest stuff in Google Spreadsheet and I can´t find any good tutorials on-line.

My question is regarding cells. In VBA to write in a cells I would do:

cells(1,1) = 2 //that would write the number 2 in the first row of the first column  

In VBA I can also assign a cell to a variable, like:

dim something as long
something = cells(1,1)

How can I apply the same principles in google SpreadSheet?

Thank you very much!

I´ve tried what you guys suggested. And now I have a follow up question.

I`m trying to modify the cells in a specific range. I´ve tried this code:

var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var board = sheet.getRange(2,2,9,9);
board[0][0].setValue(2);

But apparently I can´t use array notation. I´m not sure if .setValeu or .getRange are the problem. But is there a way I can use array notation to chance a cell?

Thanks Again!

2
  • 2
    Please google for 'google apps script' and read the tutorials there first. See SpreadsheetApp.
    – Zig Mandel
    Commented Apr 6, 2014 at 22:03
  • 1
    I know it seems I´m lazy.... but I swear it´s not the case. I´ve done the basic tutorials, but I still can´t get the hang of it... I´m sorry for being such a slow learner... Commented Apr 7, 2014 at 3:14

2 Answers 2

49

Try following:

var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var cell = sheet.getRange(1,1);
cell.setValue(2);
1
  • 2
    When using setValue() (without S) you don't need to define a range with 4 parameters, only the cell coordinates. Please update your answer.thx Commented Apr 7, 2014 at 15:09
7

It's worthwhile looking at the docs for getRange.

getRange(row, column, numRows, numColumns)

Returns the range with the top left cell at the given coordinates with the given number of rows and columns.

You can therefore update multiple cells with an array like this:

var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1, 1, numRows, numColumns).setValues(data);
SpreadsheetApp.flush();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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