Reputation: 5811
I have a very long list of the published URLs for a bunch of Google Spreadsheets. The URLs look like https://docs.google.com/a/company.com/spreadsheets/d/e/2PACX-[some very long string]/pubhtml?gid=[sheet ID]&single=true&widget=true&headers=false.
All of the source Spreadsheets are in a folder on a Shared Drive that I have access to.
I need to get the normal Drive File ID for each of the spreadsheets.
I tried to use UrlFetchApp
to open fetch the published URL file to see if it had any information I could use to extract the Drive File ID but I get a 401 error.
Is there anyway to get the internal Drive File ID from the published URL?
Upvotes: 1
Views: 2893
Reputation: 201378
https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml?gid=0&single=true&widget=true&headers=false
https://drive.google.com/drive/folders/###?usp=sharing
###
is the folder ID.If my understanding for your situation is correct, how about this workaround?
Unfortunately, the file ID cannot be directly retrieved from the published URL like https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml?gid=0&single=true&widget=true&headers=false
. I think that this might be the specification.
So from your situation, I would like to propose a workaround. The flow of this workaround is as follows. Please think of this as just one of several answers.
https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml?gid=0&single=true&widget=true&headers=false
.###
of <title>###</title>
is the filename of Spreadsheet.When above flow is reflected to the script, it becomes as follows.
When you use this script, please set the URL of the published Spreadsheet and folder ID of the shared folder.
function myFunction() {
var url = "https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml?gid=0&single=true&widget=true&headers=false"; // URL of the published Spreadsheet.
var folderId = "###"; // FolderId of shared folder including the published Spreadsheet.
var res = UrlFetchApp.fetch(url);
var filename = res.getContentText().match(/<title>(.+)<\/title>/)[1];
var files = DriveApp.getFolderById(folderId).getFiles();
while (files.hasNext()) {
var file = files.next();
if (file.getMimeType() == MimeType.GOOGLE_SHEETS && file.getName() == filename) {
Logger.log("filename: %s, fileId: %s", filename, file.getId())
}
}
}
If I misunderstood your question and this workaround was not the direction you want, I apologize.
Upvotes: 2