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

Google ドライブ: フォルダ検索 (Google Drive: Search Folder)
Google ドライブ の指定フォルダ直下に、特定の名前のフォルダがあるかどうか調べます。
2019-07-01 (C) Questetra, Inc. (MIT License)
Configs
  • C1: OAuth2設定名 *
  • C2: 検索するフォルダの親フォルダの ID (空白の場合マイドライブのルートを検索します) #{EL}
  • C3: 検索するフォルダの名称 * #{EL}
  • C4: 検索したフォルダの ID を保存するデータ項目
  • C5: 検索したフォルダの表示 URL を保存するデータ項目
Script
main();
function main(){
  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";
  }
  //get C4&C5
  const idData = configs.get("FolderIdItem");
  const urlData = configs.get("WebViewUrlItem");
  //If neither C4 nor C5 are set,throw error
  if((idData === null || idData === "") && (urlData === null || urlData === "")){
    throw "Neither of Data Items to save result are set.";
  }
  const oauth = configs.get("OAuth");

  // get OAuth token
  let token;
  token = httpClient.getOAuth2Token( oauth );
  const folderNameRep = folderName.replace(/['\\]/g,"\\$&");
  const q = "mimeType = 'application/vnd.google-apps.folder' and trashed = false and name = '" + folderNameRep + "' and '" + parentFolderId + "' in parents";
  const json = searchFolder(token,q);
  
  let folderIdList = "";
  let folderUrlList = "";
  const fileNum = json.files.length;
  if (fileNum == 0){
    throw "Folder " + folderName + " doesn't exist.";
  }
  for(let i = 0; i < fileNum; i++){
    if (i > 0){
      folderIdList += "\n";
      folderUrlList += "\n";
    }
    folderIdList += json.files[i].id;
    folderUrlList += json.files[i].webViewLink;
  }
  //set ID to Data Item
  if (idData != null && idData != "") {
    const idDataDef = engine.findDataDefinitionByNumber(idData);
    //Multiple Judge
    if(idDataDef.matchDataType("STRING_TEXTFIELD") && fileNum > 1){
      throw "Multiple folders are found.Can't set data to single-line string Data Item."
    }
    engine.setDataByNumber(idData,folderIdList);
  }
  //set URL to Data Item
  if (urlData != null && urlData != "") {
    const urlDataDef = engine.findDataDefinitionByNumber(urlData);
    //Multiple Judge
    if(urlDataDef.matchDataType("STRING_TEXTFIELD") && fileNum > 1){
      throw "Multiple folders are found.Can't set data to single-line string Data Item."
    }
    engine.setDataByNumber(urlData,folderUrlList);
  }
}
//search folder on google drive
function searchFolder(token, q) {
  const url = 'https://www.googleapis.com/drive/v3/files';
  const response = httpClient.begin()
    .bearer(token)
    .queryParam("q",q)
    .queryParam("pageSize",1000)
    .queryParam("fields", "files(id,webViewLink)")
    .queryParam("includeTeamDriveItems","true")
    .queryParam("supportsTeamDrives","true")
    .get(url);
  const status = response.getStatusCode();
  const responseTxt = response.getResponseAsString();
  if (status >= 300) {
    const error = "Failed to search \n status:" + status + "\n" + responseTxt;
    throw error;
  }
  engine.log("status:" + status + "\n" + responseTxt);
  
  jsonRes = JSON.parse(responseTxt);
  return jsonRes;
}

Download

Capture

Notes

  1. フォルダの ID は、URL に含まれています。https://drive.google.com/drive/u/1/folders/(Folder ID)
  2. フォルダが見つからない場合、エラーになります。
%d人のブロガーが「いいね」をつけました。