我想要 Javascript 正则表达式从 google 表格 URL 中提取电子表格 ID 和工作表 ID。 Sheets.google.com 电子表格的 URL 如下所示:https://docs.google.com/spreadsheets/d/1QOKbrUz7AWUwT-hNfWd1Pxf__QObYCZ1rD2CKMjpnGw/edit#gid=0 https://docs.google.com/spreadsheets/d/1QOKbrUz7AWUwT-hNfWd1Pxf__QObYCZ1rD2CKMjpnGw/edit#gid=0.
这个链接 https://developers.google.com/sheets/api/guides/concepts#spreadsheet_id描述用于提取电子表格 ID 和工作表 ID 的正则表达式。我在 Javascript 中尝试过它们,但它不起作用。这是我尝试过的代码:
var spreadsheetId = new RegExp("/spreadsheets/d/([a-zA-Z0-9-_]+)").exec(resourceUrl);
var sheetId = new RegExp("[#&]gid=([0-9]+)").exec(resourceUrl);
假设 Google 电子表格 ID 超过 15 个字符,则正则表达式将如下所示。 GID 在 URL 中可能可用,也可能不可用。
var resourceUrl = "https://docs.google.com/spreadsheets/d/1QOKbrUz7AWUwT-hNfWd1Pxf__QObYCZ1rD2CKMjpnGw/edit#gid=0";
var matches = /\/([\w-_]{15,})\/(.*?gid=(\d+))?/.exec(resourceUrl);
if (matches) {
console.log("Spreadsheet: " + matches[1]);
console.log("Sheet: " + matches[3]);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)