148

I want to set text or number in Google Sheet from script.

I want to set Hello or number 9 in cell F2. I found this code so far:

SpreadsheetApp.getActiveRange().setValue('hello');

but that doesn't specify which cell.

TheMaster
  • 37,620
  • 6
  • 43
  • 68
  • 6
    may I suggest that you read the basics of appscript by trying the [tutorials ?](https://developers.google.com/apps-script/articles) – Serge insas Jul 04 '12 at 19:36

3 Answers3

242

The following code does what is required

function doTest() {
  SpreadsheetApp.getActiveSheet().getRange('F2').setValue('Hello');
}
megabyte1024
  • 8,234
  • 4
  • 29
  • 44
  • 2
    how getRange with numbers ? what are the coordinate of the c2 cell ? – JuanPablo Jan 28 '13 at 23:03
  • 10
    C2 is third column second row so >> getRange(2,3) >> getRange(row,col) – Serge insas Mar 16 '13 at 00:37
  • Hi Do you now how to get current cell's row and col number? How can I set value to current cell? – Charles Chow Mar 02 '15 at 19:33
  • @CharlesChow, I am sorry, but I am not involved in GAS development anymore. Please refer to [this](https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#getActiveCell%28%29) and [this](https://developers.google.com/apps-script/reference/spreadsheet/range#setValue%28Object%29) help items. I think they could help. – megabyte1024 Mar 02 '15 at 19:47
  • 1
    @CharlesChow You can use SpreadsheetApp.getActiveSheet().getActiveCell().getRow() and SpreadsheetApp.getActiveSheet().getActiveCell().getColumn() – Augustine C May 26 '17 at 04:53
  • 1
    i'll do this solution but the message I receive is "you do not have the permissions needed to call setValue". What's wrong? I'll just accepted and gave the auth when I had create the srcipt. – Andrea_86 Oct 25 '17 at 09:16
52

Use setValue method of Range class to set the value of particular cell.

function storeValue() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  // ss is now the spreadsheet the script is associated with
  var sheet = ss.getSheets()[0]; // sheets are counted starting from 0
  // sheet is the first worksheet in the spreadsheet
  var cell = sheet.getRange("B2"); 
  cell.setValue(100);
}

You can also select a cell using row and column numbers.

var cell = sheet.getRange(2, 3); // here cell is C2

It's also possible to set value of multiple cells at once.

var values = [
  ["2.000", "1,000,000", "$2.99"]
];

var range = sheet.getRange("B2:D2");
range.setValues(values);
Joshua
  • 406
  • 1
  • 11
  • 18
sudo bangbang
  • 23,736
  • 11
  • 71
  • 75
23

Solution : SpreadsheetApp.getActiveSheet().getRange('F2').setValue('hello')

Explanation :

Setting value in a cell in spreadsheet to which script is attached

SpreadsheetApp.getActiveSpreadsheet().getSheetByName(SHEET_NAME).getRange(RANGE).setValue(VALUE);

Setting value in a cell in sheet which is open currently and to which script is attached

SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(RANGE).setValue(VALUE);

Setting value in a cell in some spreadsheet to which script is NOT attached (Destination sheet name known)

SpreadsheetApp.openById(SHEET_ID).getSheetByName(SHEET_NAME).getRange(RANGE).setValue(VALUE);

Setting value in a cell in some spreadsheet to which script is NOT attached (Destination sheet position known)

SpreadsheetApp.openById(SHEET_ID).getSheets()[POSITION].getRange(RANGE).setValue(VALUE);

These are constants, you must define them yourself

SHEET_ID

SHEET_NAME

POSITION

VALUE

RANGE

By script attached to a sheet I mean that script is residing in the script editor of that sheet. Not attached means not residing in the script editor of that sheet. It can be in any other place.

Umair Mohammad
  • 4,185
  • 2
  • 16
  • 33
  • Hi - Regarding to which a "Script is Active" I have an INDEX/Table formula that I want in cell D18 on Worsheet 'FianceIndex' When I do the formula in the Cell itself it doesn't auto-refresh - so I figured put it in a script and apply a trigger. Problem with getActiveSheet() is that every other sheet gets it. How do I fix that? I tried "getSheetName" SpreadsheetApp.getActiveSheet().getSheetName('FinanceIndex').getRange('D18').setValue('=Dollar(Index(ImportHTML("https://www.apmex.com/silver-price","table",8),2,2))'); I'm stuck – Robert Long Sep 05 '20 at 01:16
  • 1
    Try `SpreadsheetApp.getActiveSpreadsheet().getSheetName('FinanceIndex')`. Otherwise please ask a new question https://stackoverflow.com/help/how-to-ask – Umair Mohammad Sep 05 '20 at 06:59
  • 1
    That was it Umair! Thank you – Robert Long Sep 06 '20 at 15:54