Google ドライブ: ファイル / フォルダ削除

Google Drive: Delete File / Folder

この工程は、Google ドライブ上のファイルやフォルダを削除します。 一度に複数の削除が可能です。

Auto Step icon
Basic Configs
工程名
メモ
Configs for this Auto Step
UserID
C1: Google ドライブ に接続するユーザ(要アプリ管理権限) *
FileIdsItem
C2: 削除するファイル・フォルダ ID (1 行 1 ID) *

Notes

  • C1 のユーザは、[アカウント設定]>[Google 連携]にて、Google ドライブと連携済みである必要があります
    • ワークフロー基盤にて、Google 連携設定([システム設定]>[Google 連携])が必要です(要[システム管理権限])
  • 「Google ドライブ: ファイルアップロード」「Google ドライブ: フォルダ作成」「Google スプレッドシート: ファイル作成」で作成されたファイル・フォルダを、削除可能です。
    • 既存のファイルや他のソフトウェアが作成したファイルは削除できません。
    • 認可スコープが ​https://www.googleapis.com/auth/drive.file であるためです。
  • 削除したファイル・フォルダはゴミ箱に送られます
  • C2のデータ項目にある空行は無視されます

Capture

See also

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

main();

function main() {
    const filesIdArray = getFileIds();
    const quser = getQuser();
    for (let i = 0; i < filesIdArray.length; i++) {
        deleteFile(quser, filesIdArray[i]);
    }
}

/**
 * データ項目からファイル / フォルダ ID を取得し、配列に入れて返す
 * ID の数が通信制限を超えていればエラー
 * @return {Array<String>}  ファイル ID の配列
 */
function getFileIds() {
    const fileIds = engine.findDataByNumber(configs.get("FileIdsItem"));
    if (fileIds === null) {
        throw "File / Folder IDs aren't set.";
    }
    let fileIdsArray = fileIds.split("\n");
    fileIdsArray = fileIdsArray.filter(lines => lines !== ""); // 空文字列を削除
    if (fileIdsArray.length === 0) {
        throw "File / Folder IDs aren't set.";
    }
    if (fileIdsArray.length > httpClient.getRequestingLimit()) {
        //check number of files
        throw "Number of File IDs is over the limit.";
    }
    return fileIdsArray;
}

/**
 * 実行ユーザを取得する
 * @return {QuserView} 実行ユーザ
 */
function getQuser() {
    const quser = configs.getObject("UserID");
    if (quser === null) {
        throw "User not found.";
    }
    engine.log(`User Name: ${quser.getName()}`);
    return quser;
}

/**
 * ファイルを削除する
 * メタデータの "trashed" パラメータを true にすることでファイルをゴミ箱に入れる
 * 削除の結果をログ出力し、失敗した場合はエラーとする
 * @param {QuserView} quser  実行ユーザ
 * @param {String} fileId  削除するファイルの ID
 */
function deleteFile(quser, fileId) {
    const url = `https://www.googleapis.com/drive/v3/files/${fileId}`;
    const body = {
        trashed: true
    };
    let response = httpClient.begin()
        .googleOAuth2(quser, "Drive")
        .queryParam("supportsAllDrives", "true")
        .body(JSON.stringify(body), "application/json; charset=UTF-8")
        .patch(url);

    const status = response.getStatusCode();
    if (status >= 300) {
        engine.log(response.getResponseAsString());
        throw `Failed to delete. ID: ${fileId}, status: ${status}`;
    } else {
        engine.log(`Succeed to delete. ID: ${fileId}`);
    }
}
%d人のブロガーが「いいね」をつけました。