0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

google Driveのマイドライブにファイルがつくられてしまうので、inboxフォルダに移動されるようにする

Posted at

はじめに

spreadsheet.newやdocument.newをurlに記入してファイルを生成することが多いが、マイドライブに溜まってしまうのが煩わしかった。
そのため、ファイルを自動で

  • googleDocumentのインボックスフォルダ
  • googleSpreadsheetのインボックスフォルダ

に移動されるようにした

main.gs
function fileMoveInbox(){
  const fileList = getRootFileList();
  fileList.forEach(file => file.moveInbox());
}
util/googleDrive.gs
function getRootFileList(){
  const files = DriveApp.getRootFolder().getFiles();
  
  let fileList = [];
  while(files.hasNext()) {
    let buff = files.next();
    fileList.push(new GoogleFile(buff));
  };

  return fileList;
}
class/GoogleFile.gs
class GoogleFile{
  constructor(file){

    this.name = file.getName();
    this.type = file.getMimeType();
    this.id = file.getId();
  }

  isSpreadSheet(){
    return this.type === 'application/vnd.google-apps.spreadsheet';
  }

  isDocument(){
    return this.type === 'application/vnd.google-apps.document';
  }

  moveInbox(){
    const file = DriveApp.getFileById(this.id);

    if(this.isSpreadSheet()){
      const inbox = DriveApp.getFolderById('格納したいフォルダのid');
      file.moveTo(inbox);
    }

    if(this.isDocument()){
      const inbox = DriveApp.getFolderById('格納したいフォルダのid');
      file.moveTo(inbox);
    }
  }
}

ファイルをそのまま持たせていたほうがきれいだったかもしれない。

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?