Google AppScript: Creating File, Folder & Google Doc

This article shows how to create a new folder and file with some text inside it with Google AppScript. We will also see how to create a new Google Doc and write some text inside it through AppScript.

Create File & Folder with AppScript

The following function will create a folder name My Folder in your Google Drive and then inside that folder it will create a file named My File.txt. It will also write some text (Lorem ipsum) in that file.


/**
 * Drive Classes Overview
 * https://developers.google.com/apps-script/reference/drive/
 *
 * DriveApp Class Reference
 * https://developers.google.com/apps-script/reference/drive/drive-app
 */

function createFileFolder() {  
  // Create folder
  var folder = DriveApp.createFolder('My Folder');
  
  // Create file inside folder
  var file = folder.createFile('My File.txt', 'Lorem ipsum', MimeType.PLAIN_TEXT); 
}

Create a new Google Doc File with AppScript

In the code below, we will be creating a new Google Doc named New Doc inside the folder My Folder. “My Folder” is the folder that we just created from above code.

To copy file into a folder, first of all we need the folder id.

– Go to your Google Drive
– Go inside the folder named “My Folder”
– See the URL in your browser’s address bar
– The URL will be something like this: https://drive.google.com/drive/folders/9tXNUNpM9tXNUNp0BydbYIIYLMxxxxxxxxxx
– The string after “folders/” will be your folder ID.
– So, in this case, the folder id is “9tXNUNpM9tXNUNp0BydbYIIYLMxxxxxxxxxx”

Note:

Files cannot be created directly inside already existing folders.
So, first you have to create a file in root directory.
Then, copy the file to speficied directory.
And then, delete the file present in root directory.

In below code, we will not write anything to the newly created doc file. We will just create the new doc file inside the speficied folder in Google Drive.


/**
 * Document Classes Overview
 * https://developers.google.com/apps-script/reference/document/
 *
 * DocumentApp Class Reference 
 * https://developers.google.com/apps-script/reference/document/document-app
 */

function createDoc() {  
  var folderName = 'My Folder';
  var folderId = '9tXNUNpM9tXNUNp0BydbYIIYLMxxxxxxxxxx'; 
  var documentName = 'New Doc';  
  
  // Get folder by ID
  var folder = DriveApp.getFolderById(folderId);
  
  // Get folder by Name
  //var folder = DriveApp.getFoldersByName(folderName).next();
  
  // Create Doc
  var ss = DocumentApp.create(documentName);  
  
  // Get file by doc id
  var file = DriveApp.getFileById(ss.getId());
  
  // Get root directory
  var parents = file.getParents();
  
  // Remove file from root folder
  parents.next().removeFile(file);
  
  // Add file to the specified folder
  folder.addFile(file); 
  
}

Create new Google Doc and write some content into it with AppScript

The following code will create a new Google Doc named TestDoc and write some Headings and Paragraphs on it. The doc file will be created in root folder of Google Drive.


/**
 * Document Classes Overview
 * https://developers.google.com/apps-script/reference/document/
 *
 * DocumentApp Class Reference 
 * https://developers.google.com/apps-script/reference/document/document-app
 *
 * Extending Google Docs Guide
 * https://developers.google.com/apps-script/guides/docs
 */

function createWriteDoc() {    
  var doc = DocumentApp.create('TestDoc');
  var body = doc.getBody();
  
  // Append a document header paragraph.
  var header = body.appendParagraph("My Document");
  header.setHeading(DocumentApp.ParagraphHeading.HEADING1);
  
  // Append a regular paragraph.
  body.appendParagraph("This is a testing paragraph.")
  
  // Append a section header paragraph.
  var section = body.appendParagraph("Section 1");
  section.setHeading(DocumentApp.ParagraphHeading.HEADING2);

  // Append a regular paragraph.
  body.appendParagraph("Another Paragraph. Lorem Ipsum...");  
}

Hope this helps. Thanks.