Open Google Apps Spreadsheet by name [duplicate]

ⅰ亾dé卋堺 提交于 2021-02-11 15:26:46

问题


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..


回答1:


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.




回答2:


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:

  • open(file) documentation
  • Export Google spreadsheet to text file using script


来源:https://stackoverflow.com/questions/36868904/open-google-apps-spreadsheet-by-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!