はじめに
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);
}
}
}
ファイルをそのまま持たせていたほうがきれいだったかもしれない。