Google ドライブ: フォルダ検索

Google Drive: Search Folder

この工程は、Google ドライブ の指定フォルダ直下に、特定の名前のフォルダがあるかどうか調べます。

Auto Step icon
Basic Configs
工程名
メモ
Configs for this Auto Step
OAuth
C1: OAuth2設定 *
ParentFolderId
C2: 検索するフォルダの親フォルダの ID (空白の場合マイドライブのルートを検索します)#{EL}
FolderName
C3: 検索するフォルダの名称 *#{EL}
FolderIdItem
C4: 検索したフォルダの ID を保存するデータ項目
WebViewUrlItem
C5: 検索したフォルダの表示 URL を保存するデータ項目

Notes

  • フォルダの ID は、URL に含まれています。https://drive.google.com/drive/u/1/folders/(Folder ID)
  • フォルダが見つからない場合、エラーになります。

Capture

See also

Script (click to open)
  • 次のスクリプトが記述されている XML ファイルをダウンロードできます
    • google-drive-folder-search.xml (C) Questetra, Inc. (MIT License)
    • Professional のワークフロー基盤では、ファイル内容を改変しオリジナルのアドオン自動工程として活用できます

main();
function main() {
    //// == 工程コンフィグ・ワークフローデータの参照 / Config & Data Retrieving ==
    let parentFolderId = configs.get("ParentFolderId");
    if (parentFolderId === "" || parentFolderId === null) {
        parentFolderId = "root";
    }
    const folderName = configs.get("FolderName");
    if (folderName === "" || folderName === null) {
        throw "Folder Name is blank.";
    }
    const idDataDef = configs.getObject("FolderIdItem");
    const urlDataDef = configs.getObject("WebViewUrlItem");
    // If neither C4 nor C5 are set, throw error
    if (idDataDef === null && urlDataDef === null) {
        throw "Neither of Data Items to save result are set.";
    }
    const oauth = configs.get("OAuth");

    //// == 演算 / Calculating ==
    const driveId = getDriveId(oauth, parentFolderId);
    const folders = searchFolder(oauth, driveId, parentFolderId, folderName);
    const folderNum = folders.length;
    if (folderNum === 0) {
        throw `Could not find Folder:${folderName} with Parent Folder ID:${parentFolderId}`;
    }
    const folderIdList = [];
    const folderUrlList = [];
    for (let i = 0; i < folderNum; i++) {
        folderIdList.push(folders[i].id);
        folderUrlList.push(folders[i].webViewLink);
    }

    //// == ワークフローデータへの代入 / Data Updating ==
    setFolderData(idDataDef, folderIdList);
    setFolderData(urlDataDef, folderUrlList);
}

/**
 * 親フォルダのドライブ ID を取得する
 * @param {String} oauth OAuth2 認証設定
 * @param {String} parentFolderId 親フォルダの ID
 * @return {String} driveId ドライブ ID (共有ドライブになければ null)
 */
function getDriveId(oauth, parentFolderId) {
    if (parentFolderId === "root") {
        return null;
    }
    const url = `https://www.googleapis.com/drive/v3/files/${parentFolderId}`;
    const response = httpClient
        .begin()
        .authSetting(oauth)
        .queryParam("fields", "driveId")
        .queryParam("supportsAllDrives", "true")
        .get(url);
    const status = response.getStatusCode();
    const responseTxt = response.getResponseAsString();
    if (status >= 300) {
        const error = `Failed to get parent folder with parent folder ID:${parentFolderId}. status:${status}`;
        engine.log(responseTxt);
        throw error;
    }
    const driveId = JSON.parse(responseTxt).driveId;
    if (driveId === undefined) {
        return null;
    }
    return driveId;
}

/**
 * Google ドライブのフォルダを検索
 * @param {String} oauth OAuth2 認証設定
 * @param {String} driveId 親フォルダのドライブ ID (共有ドライブになければ null)
 * @param {String} parentFolderId 親フォルダの ID
 * @param {String} folderName 検索するフォルダの名前
 * @return {Array} folders 検索結果一覧
 * @return {String} folders[].id フォルダの ID
 * @return {String} folders[].webViewLink フォルダの表示 URL
 */
function searchFolder(oauth, driveId, parentFolderId, folderName) {
    const folderNameRep = folderName.replace(/['\\]/g, "\\$&"); // ' と \ をエスケープ
    const q = `mimeType = 'application/vnd.google-apps.folder' and trashed = false and name = '${folderNameRep}' and '${parentFolderId}' in parents`;
    const url = "https://www.googleapis.com/drive/v3/files";
    let request = httpClient
        .begin()
        .authSetting(oauth)
        .queryParam("q", q)
        .queryParam("pageSize", "1000")
        .queryParam("fields", "files(id,webViewLink)");
    if (driveId !== null) { // 親フォルダが共有ドライブにある場合
        request = request
            .queryParam("includeItemsFromAllDrives", "true")
            .queryParam("supportsAllDrives", "true")
            .queryParam("corpora", "drive")
            .queryParam("driveId", driveId);
    }
    const response = request.get(url);
    const status = response.getStatusCode();
    const responseTxt = response.getResponseAsString();
    if (status >= 300) {
        const error = `Failed to search. status:${status}`;
        engine.log(responseTxt);
        throw error;
    }
    const folders = JSON.parse(responseTxt).files;
    return folders;
}

/**
 * フォルダの情報をデータ項目にセットする
 * @param {ProcessDataDefinitionView} dataDef 保存先データ項目の ProcessDataDefinitionView
 * @param {Array<String>} folderInfoList 保存するフォルダ情報の配列
 */
function setFolderData(dataDef, folderInfoList) {
    if (dataDef !== null) {
        //Multiple Judge
        if (dataDef.matchDataType("STRING_TEXTFIELD") && folderInfoList.length > 1) {
            throw "Multiple folders are found. Can't set data to single-line string Data Item.";
        }
        const folderInfoStr = folderInfoList.join("\n");
        engine.setData(dataDef, folderInfoStr);
    }
}

%d人のブロガーが「いいね」をつけました。