我發現了一個類似的
Here the following question in regard to this problem:
- Is this the best approach to get Confluence page content in order to parse it?, for example using the option:
expand=body.storageor on contrary there are better ways to get the content of Confluence page (or specific table), so it is easier to parse. - If the content obtained is the best way to do it, then is there any HTML table javascript library or tool or script to parse the table content?
uj5u.com熱心網友回復:
我相信你的目標如下。
- 您想從問題中的示例值 (
it generates the content, but it is difficult to parse:)決議 HTML并將它們放入作業表中。- 您已經能夠從要使用的 API 中檢索值。
在這種情況下,以下示例腳本如何?在這種情況下,我想建議使用 Sheets API。Sheets API 的 pasteData 可以決議 HTML 表格。
示例腳本:
請將以下腳本復制并粘貼到 Google 電子表格的腳本編輯器中。而且,此腳本使用 Sheets API。因此,
筆記:
- 此示例腳本使用您的示例值。所以當值的結構發生變化時,這個腳本可能無法使用。所以請注意這一點。
- 當我使用您的示例值時,我可以確認此腳本作業正常。因此,當您針對實際值測驗此腳本時,發生錯誤時,請再次檢查檢索到的值。
參考:
- 方法:電子表格.batchUpdate
- 粘貼資料請求
uj5u.com熱心網友回復:
這是使用正則運算式從createResponse.getContentText(). 它不如@Tanaike 提供的解決方案通用,但它不需要在獲取所有記錄后需要洗掉的活動電子表格上生成輔助選項卡(作業表):
/*
@param id {String} Confluence ID page
*/
function getOnboardRecords(id) {
const URL = https://<COMPANY>.atlassian.net/wiki/rest/api/content/%s?expand=body.storage;
const TOKEN = "Provide the token";
const USER = "Provide user name, i.e an email";
// Relevant columns to get the information
const ROW_NAMES = ["First Name", "Last Name", "Phone Number",
"E-Mail", "Role", "Start Date", "End Date", "Vendor's Name",
"Team", "New Resource ID","New Resource Company E-mail"];
function parsePhone(s) {
let ss = s.replace(/[ \(\)\- ]/g, "");
return (ss.length == 10) ? "1" ss : ss; // Adding US code
}
function parseDate(d) {
let dateRegEx = new RegExp(/<time datetime=\\"([0-9]{4}-[0-9]{2}-[0-9]{2})\\" ?\/>/);
return d.match(dateRegEx)[1];
}
function parseEmail(e) {
let s = e.replaceAll(/<.*?>|<\/.*?>/g, "");
let validEmailRegEx = new RegExp(/^\w ([- .']\w )*@\w ([-.]\w )*\.\w ([-.]\w )*/);
const NON_VALID_EMAIL = "The input argument: '%s' is not a valid email";
if (!validEmailRegEx.test(s)) throw new Error(Utilities.formatString(NON_VALID_EMAIL, result));
return s;
}
let headers = { "Authorization": "Basic " Utilities.base64Encode(USER ':' TOKEN) };
let params = {
"method": "GET",
"headers": headers,
"muteHttpExceptions": false,
"contentType": "application/json"
};
let url = Utilities.formatString(URL, id);
let createResponse = UrlFetchApp.fetch(url, params);
let content = createResponse.getContentText();
let tableRegEx = new RegExp("<table.*?>(.*?)</table>");
let tableHtml = content.match(tableRegEx)[0];
let rowRegEx = new RegExp("<tr.*?>(.*?)</tr>", "g");
let rowsResult = tableHtml.match(rowRegEx);
let colRegExp = new RegExp("<td.*?>(.*?)</td>", "g");
let values = new Map();
rowsResult.forEach(function (item) { // parsing each column of given row
item = item.replace(/<p>|<\/p>|<p ?\/>/g, "").replace(/ /g, " ");
let row = [];
[...item.matchAll(colRegExp)].forEach(item => row.push(item[1])); // Getting the group (.*?)
row = row.filter(function (item) { return item != ""; }); // Keep non empty string only
let key = ROW_NAMES.find(it => it == row[0]);
if (key) {
if (row.length > 1) {
let records = row.slice(1);
if (key == ROW_NAMES[2]) { // phone number
records.forEach(function (item, index) {
this[index] = parsePhone(item);
}, records);
}
if ((key == ROW_NAMES[5]) || (key == ROW_NAMES[6])) {
records.forEach(function (item, index) {
this[index] = parseDate(item);
}, records);
}
if ((key == ROW_NAMES[3]) || (key == ROW_NAMES[10])) {
records.forEach(function (item, index) {
this[index] = parseEmail(item);
}, records);
}
values.set(key, records);
}
}
});
return values;
}
我正在決議每一行的特定值,因為 Confluence 在我特定的板載 Confluence 模板上具有格式。
使用特定的 Confluence ID 呼叫函式:
let onboardValues = getOnboardRecords(<ID>);
console.log(JSON.stringify([...onboardValues.entries()]))
將為具有兩個資源的入門模板生成以下輸出:
[["First Name",["FirstName1","FirstName2"]],["Last Name",
["LastName1","LastName2"]],["Phone Number",["xxxxxxxxx","xxxxxxxxx"]],
["E-Mail",["[email protected]","[email protected]"]],["Role",
["Delivery Manager","Sr. Developer"]],["Start Date",
["2021-11-01","2021-11-01"]],["End Date",["2021-11-30","2021-12-31"]],
["Vendor's Name",["xxxx","xxxxx"]],["Team",["xxxxxx]],
["New Resource ID",["xxxxxx","xxxxxx"]],
["New Resource Company E-mail",["xxxx@xxxxxx.xxx","xxxxxx@xxxx.xxx"]]]
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/367953.html
