【发布时间】:2021-04-16 18:47:35
【问题描述】:
页面导入表格的xpath是什么: https://codal.ir/Reports/Decision.aspx?LetterSerial=edVsdLrjCqoJOnLY0QVskw%3D%3D&rt=2&let=8&ct=0&ft=-1
GAS可以吗?
【问题讨论】:
标签: xml google-apps-script xpath google-sheets google-sheets-formula
页面导入表格的xpath是什么: https://codal.ir/Reports/Decision.aspx?LetterSerial=edVsdLrjCqoJOnLY0QVskw%3D%3D&rt=2&let=8&ct=0&ft=-1
GAS可以吗?
【问题讨论】:
标签: xml google-apps-script xpath google-sheets google-sheets-formula
我相信你的目标如下。
https://codal.ir/Reports/Decision.aspx?LetterSerial=edVsdLrjCqoJOnLY0QVskw%3D%3D&rt=2&let=8&ct=0&ft=-1 的URL 表。我认为在这种情况下,如何使用IMPORTHTML如下?
=IMPORTHTML(A1,"table",1)
https://codal.ir/Reports/Decision.aspx?LetterSerial=edVsdLrjCqoJOnLY0QVskw%3D%3D&rt=2&let=8&ct=0&ft=-1 的 URL 放在单元格“A1”中。使用 Google Apps 脚本时,也可以使用以下脚本。 When you use this script, please enable Sheets API at Advanced Google services。并且,请使用脚本编辑器运行myFunction 的功能。因为在这种情况下,Sheets API 用于解析 HTML 表格。不幸的是,这不能直接与自定义函数一起使用。运行此函数时,会将值放入活动工作表中。
function myFunction() {
const url = "https://codal.ir/Reports/Decision.aspx?LetterSerial=edVsdLrjCqoJOnLY0QVskw%3D%3D&rt=2&let=8&ct=0&ft=-1";
const res = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
if (res.getResponseCode() != 200) throw new Error(res.getContentText());
const table = res.getContentText().match(/<table[\s\S\w]+<\/table>/);
if (table) {
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const sheet = spreadsheet.getActiveSheet();
const resource = {requests: [{pasteData: {html: true, data: table[0], coordinate: {sheetId: sheet.getSheetId()}}}]};
Sheets.Spreadsheets.batchUpdate(resource, spreadsheet.getId());
}
}
结果:
Could not fetch url的错误。所以请注意这一点。届时,请尝试重新输入公式。通过这个,我可以获得这些值。不幸的是,我无法理解其中的详细原因。【讨论】: