google-apps-script Create a new text file and add it to the root folder


Example

 function DriveAppAddFile(child) {//Adds file to the root drive in Google Drive
  var body,returnedFolder;//Declare variable names

  if (!child) {
    body = "There is no file";
    MailApp.sendEmail(Session.getEffectiveUser().getEmail(), "", "Error Adding File!", body)
    return;
  };
    
  returnedFolder = DriveApp.addFile(child);
  
  Logger.log('returnedFolder: ' + returnedFolder);
};


function createNewFileInGoogleDrive() {
  var content,file,newFileName,timeStamp,dateTimeAsString;
  
  timeStamp = new Date();//Create a new date
  dateTimeAsString = timeStamp.toString().slice(0,15);
  
  content = "This is test file content, created at: " + dateTimeAsString;//Create content for new file
  newFileName = 'Test File ' + dateTimeAsString;//Create new file name with date/time appended to name
  
  file = DriveApp.createFile(newFileName, content);//Create a new file
  DriveAppAddFile(file);//Call a function and pass a file to the function
};