Create new file in a folder with Apps Script using Google Advanced Drive service

后端 未结 6 1696
天命终不由人
天命终不由人 2021-02-04 06:23

There are four ways to create a new file:

  • DocsList - Shown as DocsList in the Main List. Built in to Apps Script.
  • DriveApp - Shown as
6条回答
  •  北恋
    北恋 (楼主)
    2021-02-04 06:57

    The easiest way to create a new file is to use DriveApp which comes with pure Google Apps Script:

    var dir = DriveApp.getFolderById("{dir_id}");
    var file = dir.createFile(name, content);
    

    If you do not know exact directory's id you can get the folder by its name:

    var dir = DriveApp.getFoldersByName(name).next();
    

    The next() is there because getFoldersByName() returns collection of all directories whose names match given value.

    Also check DriveApp docs: https://developers.google.com/apps-script/reference/drive/drive-app

提交回复
热议问题