以下是我想要實作的目標的摘要:
- 運行在驅動器中搜索所有 Google 檔案的 Apps 腳本。
- 如果腳本找到至少包含三個單詞之一的檔案(始終在頁眉或頁腳中),則將其輸出到 Google 表格中的一行中,獲取 Google 檔案 ID、檔案名稱、指向它的鏈接,以及它在搜索中找到的詞(或者,最好包含所有者姓名)。請注意,頁眉/頁腳將只包含幾個單詞之一——永遠不會包含它們的組合。
例如,查找所有在頁眉或頁腳中包含“alice”、“bob”或“carol”的檔案;將這些條目作為單獨的行記錄在 Google 表格中。
Google 檔案 ID | 2021 年愛麗絲專案總結 | 鏈接到檔案 | “愛麗絲”
我覺得我在 Apps Script API 中找到了我需要的東西,并找到了一些類似的搜索結果,但不能完全拼湊起來。
如果這有幫助或者不清楚的話,非常樂意解釋更多!
為任何指示干杯!(即使它只是指出我的搜索不夠全面。)
uj5u.com熱心網友回復:
我相信你的目標如下。
- 您有一個 Google 電子表格。當您打開 Google 電子表格時,您希望運行一個腳本,通過檢查檔案的頁眉和頁腳并將搜索結果放入 Google 電子表格中的規格表來搜索 Google Drive 中的所有 Google 檔案檔案。
- 您想在 Google 檔案中搜索頁眉和頁腳中包含“alice”、“bob”或“carol”的值。而且,您想將值放在電子表格上。
- 您的 Google 云端硬碟中有成百上千個 Google 檔案。
- 您想使用 Google Apps 腳本來實作這一點。
在這種情況下,我想提出以下流程。
- 使用 Drive API 的搜索查詢搜索頁眉和頁腳中包含“alice”、“bob”或“carol”值的 Google 檔案檔案。
- 通過這種方法,我認為搜索成本可能會降低一點。
- 檢查每個 Google 檔案的頁眉和頁腳。當找到“alice”、“bob”或“carol”的值時,將檔案 ID、標題、鏈接和搜索值放入一個陣列中。
- 將陣列放在電子表格上。
示例腳本:
請將以下腳本復制并粘貼到 Google 電子表格的腳本編輯器中并設定作業表名稱。而且,在此示例腳本中,使用了 Drive API。因此,請在 Advanced Google services 中啟用 Drive API。
另外,請安裝 OnOpen 觸發器myFunction作為可安裝觸發器的功能。這樣,當您打開電子表格時,腳本會自動運行。在可安裝觸發器的情況下,最長執行時間為 6 分鐘,并且可以使用 Drive API。另一方面,簡單的觸發器是 30 秒。請注意這一點。
function myFunction() {
const searchTexts = ["alice", "bob", "carol"];
// 1. Search the Google Document files that the values of "alice", "bob", or "carol" are included in the header ahd footer using the search query of Drive API.
const q = `mimeType = '${MimeType.GOOGLE_DOCS}' and (` searchTexts.map(s => `fullText contains '${s}'`).join(" or ") ")";
let ar = [];
let pageToken = "";
do {
const res = Drive.Files.list({q, pageToken, maxResults: 1000, fields: "items(id, title, alternateLink)"});
if (res.items.length > 0) {
ar = ar.concat(res.items);
}
pageToken = res.nextPageToken;
} while (pageToken);
// 2. Check the header and footer of each Google Document. When the values of "alice", "bob", or "carol" are found, the Document ID, title, link and searched value are put to an array.
const values = ar.reduce((ar, {id, title, alternateLink}) => {
const doc = DocumentApp.openById(id);
let temp = [];
const header = doc.getHeader();
if (header) {
const hText = header.getText();
temp = temp.concat(searchTexts.filter(e => hText.includes(e)));
}
const footer = doc.getFooter();
if (footer) {
const fText = footer.getText();
temp = temp.concat(searchTexts.filter(e => fText.includes(e)));
}
if (temp.length > 0) ar.push([id, title, alternateLink, temp.join(",")]);
return ar;
}, []);
// 3. Put the array to Spreadsheet.
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); // Please set the sheetname.
sheet.getRange(sheet.getLastRow() 1, 1, values.length, values[0].length).setValues(values);
}
筆記:
- 在我的環境中,我沒有成百上千的 Google 檔案。而且,我無法測驗您的情況。所以請根據您的實際情況測驗上述腳本。
參考:
- 可安裝的觸發器
- 檔案:串列
- 搜索檔案和檔案夾
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/355739.html
