Google Drive: Search Folder
Search for the folder with the specific name, directly under the specified folder on Google Drive.
2019-07-01 (C) Questetra, Inc. (MIT License)
Configs
  • C1: OAuth2 Setting Name *
  • C2: Parent Folder ID (When empty, search in My Drive root) #{EL}
  • C3: Folder Name to search for * #{EL}
  • C4: Data Item that will save Folder ID
  • C5: Data Item that will save web view url of Folder
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. Folder ID is contained in the URL. https://drive.google.com/drive/u/1/folders/(Folder ID)
  2. If the folder can not be found, the execution will be failed.