GAS searchFiles使用時にエラー「Exception: Invalid argument: q」が発生した場合の対処法

GAS searchFiles使用時にエラー「Exception: Invalid argument: q」が発生した場合の対処法

GAS(Google Apps Script)で、searchFiles使用時にエラー「Exception: Invalid argument: q」が発生した場合の対処法を記述してます。存在しない日付などを設定している場合に発生します。

環境

  • OS windows11 home
  • ブラウザ chrome 107.0.5304.88

エラー全文

以下のコードを、実行時に発生

function myFunction() {

  const query = 'modifiedDate >= "2022-09-01" and modifiedDate <= "2022-11-31"';

  const files = DriveApp.searchFiles(query);

  while (files.hasNext()) {
    const file = files.next();
    Logger.log(`ファイル名${file.getName()}`);
  }

}

エラー全文

Exception: Invalid argument: q
myFunction	@ コード.gs:7

画像

原因

存在しない日付( 11月31日 )を指定しているため

対処法

存在する日付を指定する

function myFunction() {

  const query = 'modifiedDate >= "2022-09-01" and modifiedDate <= "2022-11-30"'

  const files = DriveApp.searchFiles(query);

  while (files.hasNext()) {
    const file = files.next();
    Logger.log(`ファイル名${file.getName()}`);
  }

}