productiveGuser.com

Create a New Excel File in Google Drive

Posted By: Harish
create a new Excel file in google drive folder

We have seen a way to create a text file, pdf file and even a comma separated values(CSV) file in google drive.

In this post, we will learn a way to create a Microsoft Excel file with data in a google drive folder using apps script.

Open Google Apps Script editor and create a new project.

Replace the default function (which already exists) with the below function and run it. Visit creating and executing a Google Apps Script post for more info.

function createMSExcelFileinGoogleDrive() {
  try {
    let folder;
    const folderName = "Excel Folder"; //<-- change folder name of your choice
    const excelFolder = DriveApp.getFoldersByName(folderName);
    const isFolder = excelFolder.hasNext();
    if (!isFolder) {
      folder = DriveApp.createFolder(folderName);
    } else {
      folder = excelFolder.next();
    };
    const createdFile = folder.createFile(
      "productiveGuser Excel File",
      `NAME, EMAIL,
       harish, [email protected],
       test one, [email protected]
      `, //<-- add Excel content here
      MimeType.MICROSOFT_EXCEL //MimeType.MICROSOFT_EXCEL_LEGACY
    );
    const url = createdFile.getUrl()
    Logger.log(url)
  } catch (err) {
    Logger.log(err)
  }
}

By running the above function, a new excel(.xlsx) file with data will be created inside the folder.

If you want an old excel extension i.e, .xls format, use MimeType.MICROSOFT_EXCEL_LEGACY.

Above function can be used to create a simple excel file with less data.

After successful execution of the above function, the url of the newly created excel file will be printed out in the execution log window. Use that link to directly open the file.

Share is Caring

Related Posts