LoginSignup
1
1

More than 1 year has passed since last update.

40 代おっさん GASのFileクラスについて学ぶ

Posted at

本記事ついて

本記事は プログラミング初学者の私が学習していく中でわからない単語や概要をなるべくわかりやすい様にまとめたものです。
もし誤りなどありましたらコメントにてお知らせいただけるとありがたいです。

Fileクラス

Fileクラスとは
ファイルを操作する機能を提供するクラス

*Fileクラスの主なメンバーの図は、参考資料の本を見ていただくか、ネットで調べください。

ちなみにこちらで書いてありました。
https://excel-ubara.com/apps_script1/GAS036.html

お試し

function tosiki() {
  const id     = '****************';
  const file   = DriveApp.getFileById(id);

  console.log(file.getId());
  console.log(file.getName());
  console.log(file.getDescription());
  console.log(file.getMimeType());
  console.log(file.getSize());
  console.log(file.getSize());

  console.log(file.getUrl());
  console.log(file.getDownloadUrl());

  console.log(file.getDateCreated());
  console.log(file.getLastUpdated());

  console.log(file.isStarred());
  console.log(file.isTrashed());
}

ファイルのコピー・移動・削除

ファイル移動
moveToメソッドを使用

構文

Fileオブジェクト.moveTo(フォルダ)

ファイルをゴミ箱に移動
setTrashedメソッドを使用

構文

Fileオブジェクト.setTrashed(真偽値)

お試し

function tosiki() {
  const id     = '**********************';
  const file   = DriveApp.getFileById(id);

  const movedFile     = file.makeCopy('tosiki.txt');
  const destinationId = '1XzpL7AEzOCRunIc4NCI60MTFbWbWpaFM';
  const destination   = DriveApp.getFolderById(destinationId);
  movedFile.moveTo(destination);

  const trasheFile = file.makeCopy('tosiki.txt');
  trasheFile.setTrashed(true);
}

参考資料

https://www.amazon.co.jp/%E8%A9%B3%E8%A7%A3-Google-Apps-Script%E5%AE%8C%E5%85%A8%E5%85%A5%E9%96%80-%E7%AC%AC3%E7%89%88/dp/4798064742

1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1