2

I see similar questionns has been asked multiple times before, but I cant seem to get them to work. I've also read that Google changed how their URLs are built up, so most of the solutions were deprecated unfortunately.

I'm looking for a link to a specific sheet of a workbook that has been published. I've made a simple workbook to test, and the published link looks like this: https://docs.google.com/spreadsheets/d/e/2PACX-1vRrmEbjecLvXhbm409pa6JJXZd_ZXTG8Zt6OevIUs5Axq5oxlCZKU0QXk-2lW05HyXJ2B4Bzy3bG-4L/pubhtml

As you can see there is a top menu to change between the sheets, but that doesn't affect the URL.

Is there any way I can get a URL to "Sheet2" directly? Or is that dependant on having the Sheet ID (I'm not the owner of said spreadsheet)?

5
  • In order to directly open the sheet except for the 1st tab, it is required to know the sheet ID of the sheet. Unfortunately, 2PACX-### is not the Spreadsheet ID. So in order to retrieve the sheet ID, Spreadsheet ID is required. And also, from your question, it seems that the Spreadsheet is not yours. In this case, the Spreadsheet is required to be shared publicly. In this case, it is not the web publish. Please be careful this. So can you retrieve the Spreadsheet ID?
    – Tanaike
    Apr 26, 2020 at 8:25
  • I only have access to the web publish, not the Spreadsheet ID. So this seems impossible, even though I have access to the sheet in question I cant manage to link directly to it/access it directly.
    – Mixy
    Apr 26, 2020 at 8:48
  • Thank you for replying. In that case, I think that the sheet ID cannot be retrieved. By this, I think that the direct link for the sheet except for the 1st tab cannot be retrieved. I apologize for not coming up with other workaround yet.
    – Tanaike
    Apr 26, 2020 at 9:03
  • Thanks for getting back to me. Thats unfortunate and kind of a weird limitation in my eyes.
    – Mixy
    Apr 26, 2020 at 18:44
  • Thank you for replying. I came up with a workaround for achieving your goal. I posted it as an answer. Could you please confirm it? If that was not the direction you want, I apologize.
    – Tanaike
    Apr 27, 2020 at 0:09

2 Answers 2

4

I believe your goal as follows.

  • You want to retrieve the values from Sheet2 from the URL of https://docs.google.com/spreadsheets/d/e/2PACX-1vRrmEbjecLvXhbm409pa6JJXZd_ZXTG8Zt6OevIUs5Axq5oxlCZKU0QXk-2lW05HyXJ2B4Bzy3bG-4L/pubhtml.
  • The owner of this Spreadsheet is not you.
  • You don't know the Spreadsheet ID and each sheet ID in the Spreadsheet. You know only the URL of https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml.
  • Under above situation, you want to retrieve the direct URL of the sheet 2.

For above goal, how about this answer?

Issue and workarounds:

Unfortunately, in the current stage, it seems that the Spreadsheet ID and each sheet ID cannot be directly retrieved from the URL of https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml. I think that this is the current specification. Also I think that this reason might be due to the security. So in order to achieve your goal, it is required to think of the workaround.

In this answer, as a workaround, I would like to achieve your goal using Web Apps created by Google Apps Script. When Web Apps is used, the directlink of Sheet2 can be retrieved.

Flow:

The flow of this workaround is as follows.

  1. Download the Google Spreadsheet as a XLSX data from the URL of https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml.
  2. Convert the XLSX data to Google Spreadsheet.
  3. Publish the converted Google Spreadsheet to Web.
  4. Retrieve the URLs of each sheet.

Usage:

Please do the following flow.

1. Create new project of Google Apps Script.

Sample script of Web Apps is a Google Apps Script. So please create a project of Google Apps Script.

If you want to directly create it, please access to https://script.new/. In this case, if you are not logged in Google, the log in screen is opened. So please log in to Google. By this, the script editor of Google Apps Script is opened.

2. Prepare script.

Please copy and paste the following script (Google Apps Script) to the script editor. And please enable Google Drive API at Advanced Google services. This script is for the Web Apps.

function doGet(e) {
  const prop = PropertiesService.getScriptProperties();
  const ssId = prop.getProperty("ssId");
  if (ssId) {
    DriveApp.getFileById(ssId).setTrashed(true);
    prop.deleteProperty("ssId");
  }
  const inputUrl = e.parameter.url;
  const re = new RegExp("(https?:\\/\\/docs\\.google\\.com\\/spreadsheets\\/d\\/e\\/2PACX-.+?\\/)");
  if (!re.test(inputUrl)) return ContentService.createTextOutput("Wrong URL.");
  const url = `${inputUrl.match(re)[1]}pub?output=xlsx`;
  const blob = UrlFetchApp.fetch(url).getBlob();
  const id = Drive.Files.insert({mimeType: MimeType.GOOGLE_SHEETS, title: "temp"}, blob).id;
  prop.setProperty("ssId", id);
  Drive.Revisions.update({published: true, publishedOutsideDomain: true, publishAuto: true}, id, 1);
  const sheets = SpreadsheetApp.openById(id).getSheets();
  const pubUrls = sheets.map(s => ({[s.getSheetName()]: `https://docs.google.com/spreadsheets/d/${id}/pubhtml?gid=${s.getSheetId()}`}));
  return ContentService.createTextOutput(JSON.stringify(pubUrls)).setMimeType(ContentService.MimeType.JSON);
}
  • In this case, the GET method is used.
  • In this script, when the below curl command is run, the Google Spreadsheet is downloaded as a XLSX data, and the XLSX data is converted to Google Spreadsheet. Then, the converted Spreadsheet is published to the web. By this, the direct links of each sheet can be retrieved.
    • Also, in this script, it supposes that the original Spreadsheet is changed. So if you run the curl command again, the existing Spreadsheet is deleted and new Spreadsheet is created by downloading from the original Spreadsheet. In this case, the URLs are updated.
    • So if the Spreadsheet is not changed, you can continue to use the retrieved URLs. Of course, you can also directly use the downloaded and converted Spreadsheet.

3. Deploy Web Apps.

  1. On the script editor, Open a dialog box by "Publish" -> "Deploy as web app".
  2. Select "Me" for "Execute the app as:".
    • By this, the script is run as the owner.
  3. Select "Anyone, even anonymous" for "Who has access to the app:".
    • In this case, no access token is required to be request. I think that I recommend this setting for your goal.
    • Of course, you can also use the access token. At that time, please set this to "Anyone".
  4. Click "Deploy" button as new "Project version".
  5. Automatically open a dialog box of "Authorization required".
    1. Click "Review Permissions".
    2. Select own account.
    3. Click "Advanced" at "This app isn't verified".
    4. Click "Go to ### project name ###(unsafe)"
    5. Click "Allow" button.
  6. Click "OK".
  7. Copy the URL of Web Apps. It's like https://script.google.com/macros/s/###/exec.
    • When you modified the Google Apps Script, please redeploy as new version. By this, the modified script is reflected to Web Apps. Please be careful this.

4. Run the function using Web Apps.

This is a sample curl command for requesting Web Apps. Please set your Web Apps URL.

curl -L "https://script.google.com/macros/s/###/exec?url=https://docs.google.com/spreadsheets/d/e/2PACX-1vRrmEbjecLvXhbm409pa6JJXZd_ZXTG8Zt6OevIUs5Axq5oxlCZKU0QXk-2lW05HyXJ2B4Bzy3bG-4L/pubhtml"
  • In this case, the GET method is used at Web Apps side. So you can also directly access to the above URL using your browser.

Note:

  • When you modified the script of Web Apps, please redeploy the Web Apps as new version. By this, the latest script is reflected to the Web Apps. Please be careful this.
  • In this answer, I thought that you might use this from outside. So I used Web Apps. If you want to directly retrieved from the Google Apps Script, you can also use the following script.

    function myFunction() {
      const inputUrl = "https://docs.google.com/spreadsheets/d/e/2PACX-1vRrmEbjecLvXhbm409pa6JJXZd_ZXTG8Zt6OevIUs5Axq5oxlCZKU0QXk-2lW05HyXJ2B4Bzy3bG-4L/pubhtml";
    
      const prop = PropertiesService.getScriptProperties();
      const ssId = prop.getProperty("ssId");
      if (ssId) {
        DriveApp.getFileById(ssId).setTrashed(true);
        prop.deleteProperty("ssId");
      }
      const re = new RegExp("(https?:\\/\\/docs\\.google\\.com\\/spreadsheets\\/d\\/e\\/2PACX-.+?\\/)");
      if (!re.test(inputUrl)) throw new Error("Wrong URL.");
      const url = `${inputUrl.match(re)[1]}pub?output=xlsx`;
      const blob = UrlFetchApp.fetch(url).getBlob();
      const id = Drive.Files.insert({mimeType: MimeType.GOOGLE_SHEETS, title: "temp"}, blob).id;
      prop.setProperty("ssId", id);
      Drive.Revisions.update({published: true, publishedOutsideDomain: true, publishAuto: true}, id, 1);
      const sheets = SpreadsheetApp.openById(id).getSheets();
      const pubUrls = sheets.map(s => ({[s.getSheetName()]: `https://docs.google.com/spreadsheets/d/${id}/pubhtml?gid=${s.getSheetId()}`}));
      console.log(pubUrls);  // You can see the URLs for each sheet at the log.
    }
    

References:

Added:

As another workaround, when the original Spreadsheet is often changed, and the number of sheet is constant in the original Spreadsheet, and then, you want to retrieve only values, you can also use the following script. In this script, the URL is not changed even when the script is run again. So you can continue to use the URL.

Sample script:

function myFunction() {
  const inputUrl = "https://docs.google.com/spreadsheets/d/e/2PACX-1vRrmEbjecLvXhbm409pa6JJXZd_ZXTG8Zt6OevIUs5Axq5oxlCZKU0QXk-2lW05HyXJ2B4Bzy3bG-4L/pubhtml";

  const re = new RegExp("(https?:\\/\\/docs\\.google\\.com\\/spreadsheets\\/d\\/e\\/2PACX-.+?\\/)");
  if (!re.test(inputUrl)) throw new Error("Wrong URL.");
  const url = `${inputUrl.match(re)[1]}pub?output=xlsx`;
  const blob = UrlFetchApp.fetch(url).getBlob();
  const prop = PropertiesService.getScriptProperties();
  let sheets;
  let ssId = prop.getProperty("ssId");
  if (ssId) {
    const temp = Drive.Files.insert({mimeType: MimeType.GOOGLE_SHEETS, title: "tempSpreadsheet"}, blob).id;
    const tempSheets = SpreadsheetApp.openById(temp).getSheets();
    sheets = SpreadsheetApp.openById(ssId).getSheets();
    tempSheets.forEach((e, i) => {
      const values = e.getDataRange().getValues();
      sheets[i].getRange(1, 1, values.length, values[0].length).setValues(values);
    });
    DriveApp.getFileById(temp).setTrashed(true);
  } else {
    ssId = Drive.Files.insert({mimeType: MimeType.GOOGLE_SHEETS, title: "copiedSpreadsheet"}, blob).id;
    Drive.Revisions.update({published: true, publishedOutsideDomain: true, publishAuto: true}, ssId, 1);
    prop.setProperty("ssId", ssId);
    sheets = SpreadsheetApp.openById(ssId).getSheets();
  }
  const pubUrls = sheets.map(s => ({[s.getSheetName()]: `https://docs.google.com/spreadsheets/d/${ssId}/pubhtml?gid=${s.getSheetId()}`}));
  console.log(pubUrls);  // You can see the URLs for each sheet at the log.
}
14
  • Wow, thank you so much for this thorough answer! Unfortunately, although the initial assumptions and goals are right, I dont think I can download an xlsx from the "2-PACKX-##/pubhtml" URL. There is no top menu. And even if I could I'd say this solution is more advanced than I was looking for to be honost. Even though I can understand most of it, this seems like a pretty heavy workaround. Thanks again, sorry if this is disappointing.
    – Mixy
    Apr 27, 2020 at 13:13
  • 2
    @Mixy Thank you for replying. I think that you might misunderstand my answer. In order to download the Google Spreadsheet of the URL of 2-PACKX-## as the XLSX data, this can be achieved by modifying the URL. This can be seen at my script. In your URL, you can download it as the XLSX data by accessing to https://docs.google.com/spreadsheets/d/e/2PACX-1vRrmEbjecLvXhbm409pa6JJXZd_ZXTG8Zt6OevIUs5Axq5oxlCZKU0QXk-2lW05HyXJ2B4Bzy3bG-4L/pub?output=xlsx. By this, all sheets are inluded. So I proposed this workaround.
    – Tanaike
    Apr 27, 2020 at 22:11
  • I see. That works on the spreadsheet I created to mock up of the issue, but not on the original spreadsheet I'm trying to scrape. Hope you understand I dont want to post the original link here. Getting a File not found error with the link format /pub?output=xlsx After all looks like I have to find a new solution. Excel is crumbling with all the queries I'm adding so I have to find something more efficient I think
    – Mixy
    Apr 28, 2020 at 20:59
  • @Mixy Thank you for replying. I apologize for the inconvenience. I cannot understand about Getting a File not found error with the link format /pub?output=xlsx. Can I ask you about the detail of it?
    – Tanaike
    Apr 28, 2020 at 23:49
  • 1
    I've created a temporary URL for the original sheet. temporary-url.com/0A266E Would you check for an endpoint to get xlsx-download from this sheet please?
    – Mixy
    Apr 29, 2020 at 7:36
0

For anyone else, there might be another way to do this. You could use the gid query param present in the normal URL of the Gsheet for your selected sheet and append it to the end of your link like this https://docs.google.com/spreadsheets/d/e/2PACX-1vRrmEbjecLvXhbm409pa6JJXZd_ZXTG8Zt6OevIUs5Axq5oxlCZKU0QXk-2lW05HyXJ2B4Bzy3bG-4L/pubhtml?gid=123456

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.