0

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?

IMTheNachoMan
  • 5,343
  • 5
  • 40
  • 89
  • I've found this https://stackoverflow.com/questions/16840038/easiest-way-to-get-file-id-from-url-on-google-apps-script, hope that helps. – Mario R. Jun 30 '19 at 06:00

1 Answers1

1
  • You know the URL of published Spreadsheet. The URL is like below.
    • https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml?gid=0&single=true&widget=true&headers=false
  • The published Spreadsheet is put in the shared folder with you.
  • You know the folder ID of the shared folder. The URL is like below.
    • https://drive.google.com/drive/folders/###?usp=sharing
    • ### is the folder ID.
  • You can access to the shared folder.

If my understanding for your situation is correct, how about this workaround?

Issue:

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.

Workaround:

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.

Flow:

  1. Retrieve HTML data from the URL of https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml?gid=0&single=true&widget=true&headers=false.
  2. Retrieve the filename from the retrieved HTML data.
    • In this case, ### of <title>###</title> is the filename of Spreadsheet.
  3. Retrieve files from the shared folder.
  4. Retrieve the file ID from the filename.

When above flow is reflected to the script, it becomes as follows.

Sample script:

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())
    }
  }
}

Note:

  • In this workaround, the file ID is retrieved from the filename. So when there are several files with the same filename in the shared folder, several file IDs are retrieved. At that time, please retrieve the file ID you want by comparing the data of each Spreadsheet.

References:

If I misunderstood your question and this workaround was not the direction you want, I apologize.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • If I try what you have, the `UrlFetchApp` returns a login page. If I try `UrlFetchApp.fetch(url, {headers: {Authorization: 'Bearer ' + ScriptApp.getOAuthToken()}});` it says unauthorized. I can open the URL from my browser so I know it works.... – IMTheNachoMan Jun 30 '19 at 15:53
  • So I think I hacked together a solution. I opened one of the published sheets in Chrome and then from the developer console I can make an `XMLHttpRequest` call to all of the published URLs. So I just wrote some code in the developer console to call all of the published URLs and write the title to the console log. Its a hack but it seems to be working. – IMTheNachoMan Jun 30 '19 at 17:40
  • @IMTheNachoMan Thank you for replying. I'm glad your issue was resolved by your script. For my answer, I have to apologize for the inconvenience. In order to confirm your situation, can you provide the URL of the sample Spreadsheet which are published to web and publicly shared? If you can do, I would like to test it. Because in my environment, when the Spreadsheet is published and shared with me, above script worked. I thought that the reason of this issue might be also useful for other users. – Tanaike Jun 30 '19 at 22:09