productiveGuser.com

Create a New CSV File in Google Drive

Posted By: Harish
create a new Comma Separated Values file in google drive folder

We have seen a way to create a text file and to create a pdf file in Google Drive.

In this post, we will directly create a CSV 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 createCSVinGoogleDrive() {
  try {
    let folder;
    const folderName = "CSV Folder"; //<-- change folder name of your choice
    const csvFolder = DriveApp.getFoldersByName(folderName);
    const isFolder = csvFolder.hasNext();
    if (!isFolder) {
      folder = DriveApp.createFolder(folderName);
    } else {
      folder = csvFolder.next();
    };
    const createdFile = folder.createFile(
      "productiveGuser CSV File",
      `NAME, EMAIL,
       harish, [email protected],
       test one, [email protected]
      `, //<-- add CSV content here
      MimeType.CSV
    );
    const url = createdFile.getUrl()
    Logger.log(url)
  } catch (err) {
    Logger.log(err)
  }
}

By running the above function, a new CSV file with data will be added to a folder.

We will get the URL of the newly created CSV file in the execution log. Use that link to directly open the file.

Share is Caring

Related Posts