0

With a script I create a new Google Spreadsheet every day. I'm certain there is only one spreadsheet on the drive with this name, as I trash everything with the same name before creating a new one.

But how can I open the spreadsheet with google apps script? I have no ID for the file. I have a small script so I can find the file:

function myFunction() {
 // Log the name of every file in the user's Drive.
var files = DriveApp.getFilesByName('datafile');
while (files.hasNext()) {
var file = files.next();
Logger.log(file.getName());
}
}

Is it possible to open this spreadsheet at all with a script? I want to download it everyday on a server as csv file..

Arie Osdorp
  • 642
  • 2
  • 8
  • 17

2 Answers2

2

You can use the PropertiesService to store the id of the spreadsheet when you create it:

var id = newSS.getId(); //where newSS is your new spreadsheet
PropertiesService.getScriptProperties().setProperty('id', id);

You can then call the stored property when you want it:

PropertiesService.getScriptProperties().getProperty('id');

For more information, check out the PropertiesService documentation.

Silinus
  • 168
  • 4
0

Yes, you can open a spreadsheet, having the corresponding file object.

function myFunction() {
  var files = DriveApp.getFilesByName('datafile');
  while (files.hasNext()) {
    var file = files.next();
    var ss = SpreadsheetApp.open(file); 
    // do something with spreadsheet
  }
}

References:

Community
  • 1
  • 1