问题
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