How to get the correct range to set the value to a cell?

后端 未结 3 1132
醉酒成梦
醉酒成梦 2020-11-30 19:48

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 fa

3条回答
  •  渐次进展
    2020-11-30 20:12

    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.

提交回复
热议问题