我相信你的目标如下。
- 您想从
https://docs.google.com/spreadsheets/d/e/2PACX-1vRrmEbjecLvXhbm409pa6JJXZd_ZXTG8Zt6OevIUs5Axq5oxlCZKU0QXk-2lW05HyXJ2B4Bzy3bG-4L/pubhtml 的 URL 中检索来自 Sheet2 的值。
- 此电子表格的所有者不是您。
- 您不知道电子表格 ID 和电子表格中的每个表格 ID。你只知道
https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml的网址。
- 在上述情况下,您要检索工作表 2 的直接 URL。
对于以上目标,这个答案怎么样?
问题和解决方法:
不幸的是,在现阶段,似乎无法从https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml的URL中直接检索到Spreadsheet ID和每个sheet ID。我认为这是当前的规范。我也认为这个原因可能是由于安全性。因此,为了实现您的目标,需要考虑解决方法。
在此答案中,作为一种解决方法,我想使用由 Google Apps 脚本创建的 Web 应用程序来实现您的目标。使用Web Apps时,可以检索到Sheet2的直连。
流程:
此变通方法的流程如下。
- 从
https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml 的 URL 下载 Google 电子表格作为 XLSX 数据。
- 将 XLSX 数据转换为 Google 电子表格。
- 将转换后的 Google 电子表格发布到网络。
- 检索每张工作表的 URL。
用法:
请执行以下流程。
1。创建 Google Apps 脚本的新项目。
Web Apps 的示例脚本是 Google Apps 脚本。所以请创建一个 Google Apps Script 项目。
如果要直接创建,请访问https://script.new/。在这种情况下,如果您没有登录 Google,则会打开登录屏幕。所以请登录谷歌。这样,Google Apps Script 的脚本编辑器就打开了。
2。准备脚本。
请将以下脚本(Google Apps 脚本)复制并粘贴到脚本编辑器中。还有please enable Google Drive API at Advanced Google services。此脚本适用于 Web 应用程序。
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);
}
- 在这种情况下,使用 GET 方法。
- 在此脚本中,当运行以下 curl 命令时,Google 电子表格将下载为 XLSX 数据,并将 XLSX 数据转换为 Google 电子表格。然后,将转换后的电子表格发布到 Web。这样,可以检索每个工作表的直接链接。
- 此外,在此脚本中,它假设原始电子表格已更改。因此,如果再次运行 curl 命令,则会删除现有的电子表格,并通过从原始电子表格下载来创建新的电子表格。在这种情况下,会更新 URL。
- 因此,如果电子表格未更改,您可以继续使用检索到的 URL。当然,您也可以直接使用下载并转换后的电子表格。
3。部署 Web 应用程序。
- 在脚本编辑器上,通过“发布”->“部署为 Web 应用”打开一个对话框。
- 为“执行应用程序为:”选择“我”。
- 为“谁有权访问应用程序:”选择“任何人,甚至匿名”。
- 在这种情况下,不需要请求访问令牌。我认为我建议您使用此设置来实现您的目标。
- 当然,您也可以使用访问令牌。届时,请将其设为“任何人”。
- 单击“部署”按钮作为新的“项目版本”。
- 自动打开“需要授权”对话框。
- 点击“查看权限”。
- 选择自己的帐户。
- 点击“此应用未验证”中的“高级”。
- 点击“转到###项目名称###(不安全)”
- 点击“允许”按钮。
- 点击“确定”。
- 复制 Web 应用程序的 URL。就像
https://script.google.com/macros/s/###/exec。
- 当您修改 Google Apps 脚本时,请重新部署为新版本。这样,修改后的脚本就会反映到 Web 应用程序中。请注意这一点。
4。使用 Web 应用程序运行函数。
这是一个用于请求 Web 应用程序的示例 curl 命令。请设置您的网络应用 URL。
curl -L "https://script.google.com/macros/s/###/exec?url=https://docs.google.com/spreadsheets/d/e/2PACX-1vRrmEbjecLvXhbm409pa6JJXZd_ZXTG8Zt6OevIUs5Axq5oxlCZKU0QXk-2lW05HyXJ2B4Bzy3bG-4L/pubhtml"
- 在这种情况下,在 Web 应用端使用 GET 方法。所以您也可以使用浏览器直接访问上述网址。
注意:
- 当您修改 Web Apps 的脚本时,请将 Web Apps 重新部署为新版本。这样,最新的脚本就会反映到 Web 应用程序中。请注意这一点。
-
在这个答案中,我认为您可以从外部使用它。所以我使用了网络应用程序。如果要直接从 Google Apps 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.
}
参考资料:
补充:
作为另一种解决方法,当原始电子表格经常更改,并且原始电子表格中的工作表数量是恒定的,然后,您只想检索值,您也可以使用以下脚本。在此脚本中,即使再次运行脚本,URL 也不会更改。这样您就可以继续使用该 URL。
示例脚本:
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.
}