Google ドライブ: フォルダ作成 (Google Drive: Create Folder)
Google ドライブ の指定フォルダ内に新しいフォルダを作成します
https://support.questetra.com/ja/addons/googledrive-foldercreate/
2019-01-18 (C) Questetra, Inc. (MIT License)
Configs
  • C1: Google ドライブ に接続するユーザ *
  • I1: 作成するフォルダの親フォルダの ID (空白の場合マイドライブのルートに作成されます) #{EL}
  • I2: 作成するフォルダの名称 * #{EL}
  • O1: 作成したフォルダの ID を保存するデータ項目
  • O2: 作成したフォルダの表示 URL を保存するデータ項目
Script
main();
function main(){
  let parentFolderId = configs.get("ParentFolderId");
  const folderName = configs.get("FolderName");
  if (folderName == "" ||folderName == null) {
    throw "Folder Name is blank";
  }
  
  const userId = configs.get("UserID");
  const quser = quserDao.findById(parseInt(userId));
  if (quser == null) {
    throw "User not found:" + userId;
  }
  engine.log("User Name:" + quser.getName());
  // get OAuth token
  let token;
  try{
    token = httpClient.getGoogleOAuth2Token(quser, "Drive");
  }catch(e){
    throw "This User has not connected with Google Drive."
  }
  const json = createFolder(token, parentFolderId, folderName);

  setData("FolderIdItem", json, "id");
  setData("WebViewUrlItem", json, "webViewLink");
}
  //save Data to process data
function setData(configName, json, property) {
  const item = configs.get(configName);
  if (item == null || item == "") {
    return;
  }
  const data = json[property];
  engine.setDataByNumber(item, data);
}
  //create folder on google drive
function createFolder(token, parentFolderId, name) {
  let jsonRes = {};
  //mime type of google drive folder
  jsonRes["mimeType"] = "application/vnd.google-apps.folder";
  if (parentFolderId !== "" && parentFolderId !== null){
    jsonRes["parents"] = [ parentFolderId + '' ];
  }
  jsonRes["name"] = name + '';
  
  const url = 'https://www.googleapis.com/drive/v3/files/';
  const response = httpClient.begin()
    .bearer(token)
    .queryParam("fields", "id, webViewLink")
    .queryParam("supportsTeamDrives","true")
    .body(JSON.stringify(jsonRes), "application/json; charset=UTF-8")
    .post(url);
  const status = response.getStatusCode();
  const responseTxt = response.getResponseAsString();
  if (status >= 300) {
    const error = "Failed to create \n status:" + status + "\n" + responseTxt
    throw error;
  }
  engine.log("status:" + status);
  engine.log(responseTxt);
  
  jsonRes = JSON.parse(responseTxt);
  return jsonRes;
}

Download

google-drive-folder-create.xml

Capture

Notes

  1. C1 のユーザは、[アカウント設定]>[Google 連携]にて、Google ドライブと連携済みである必要があります

See also