Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

google apps script - Get the spreadsheet key that is in the URL. Not ss.getId()

How to get the spreadsheet ID that is in the URL. I know about using SpreadsheetApp.getActiveSpreadsheet().getId() and SpreadsheetApp.getActiveSpreadsheet().getUrl()

But getId()/getUrl()/getKey() now fails with Google-Drive-SDK. I am no longer able to use this ID to open files with the Drive-SDK. It was working about 1 week ago, but the Drive-SDK now fails to open the file.

A workaround is to use the ID that is in the URL. The id after key=

https://docs.google.com/spreadsheet/ccc?key=0AkGlO9jJLGO8dHJrSE0wTEF2VXRSdGRlNVQaaVRad0E

But I want an automated way to get it, not a manual process. As the spreadsheet is being created using a template, so I don't have a way to know the ID. Thoughts, is there a way to get it?

Note 1: stackoverflow.com/questions/18583885/ says Google Drive SDK issues should be posted to stackoverflow

Note 2: getUrl() and getKey() contains the same ID as getId() not the key in the URL. So it looks like access to the key has been removed, so am not holding out much hope.

Edit

The drive code, is Java rather than google-apps-script. version 1.15.0-rc

    Drive driveService = new Drive.Builder(TRANSPORT, JSON_FACTORY, credential).setApplicationName("CellMaster.com.au").build();
    File file = driveService.files().get(this.spreadsheetKey).execute();

I think it broke sometime in the last week or so. It fails with the execute(), it produces a 404 error. It gives the error when I use with the short spreadsheetKey that is produced by google apps script. But it still works fine when I use the browser URL key value.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't show the code that is not working for you, but the following works ok for me

function myFunction() {
// create a new spreadsheet workbook
var ss = SpreadsheetApp.create("Test");
var id = ss.getId();
Logger.log(id)
// get the ss file through Drive using spreadsheet id
var file = DriveApp.getFileById(id);
var driveid = file.getId();
Logger.log(driveid)
// open the spreadsheet using id of file
var ssfromdrive = SpreadsheetApp.openById(driveid);
Logger.log(ssfromdrive.getName())
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...