Home > Net >  How to set value in a cell in a spreadsheet which is not open (and only file name is known)
How to set value in a cell in a spreadsheet which is not open (and only file name is known)

Time:11-22

I only know the name of that file (it is a Google sheet file) and not the file id. Not possible to get file id manually. Also, there is only one file with this name in my drive. The name is a "abc". So, no iterations required to find this file. The file is not open.

I want to set the value of cell D1 in this file to file name, which is "abc".

I am a beginner and I would need entire code. Please help.

My status:

//get sheet id                                                  
var sheetid = DriveApp.getFilesByName('abc').next().getId();

//change cell d1
SpreadsheetApp.openById(sheetid).getSheetByName('abc').getRange('D1').setValue('abc');
}

Result = Error = TypeError: Cannot read property 'getRange' of null

CodePudding user response:

Your .getRange() is null most likely because your .getSheetByName('abc') is incorrect.

  1. If you know there is only one sheet within this file you can remove .getSheetByName('abc') and it will work.
  2. If you know the sheet name within the file, replace 'abc' with that name.

https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#getSheetByName(String)

  • Related