当前位置:首页 > 未分类 > 正文内容

How to Import CSV Files into Google Spreadsheets with Google Apps Script

ceacer3周前 (05-02)未分类4723

You can easily import CSV files into Google Spreadsheet using the Utilities.parseCsv() method of Google Apps Script. The snippets here show how to parse and import data from CSV files that are on the web, saved on Google Drive or available as a Gmail attachments.

Import CSV from an email attachment in Gmail

function importCSVFromGmail() {
  var threads = GmailApp.search('from:[email protected]');
  var message = threads[0].getMessages()[0];
  var attachment = message.getAttachments()[0];

  // Is the attachment a CSV file
  if (attachment.getContentType() === 'text/csv') {
    var sheet = SpreadsheetApp.getActiveSheet();
    var csvData = Utilities.parseCsv(attachment.getDataAsString(), ',');

    // Remember to clear the content of the sheet before importing new data
    sheet.clearContents().clearFormats();
    sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
  }
}

Import CSV file that exists in Google Drive**

function importCSVFromGoogleDrive() {
  var file = DriveApp.getFilesByName('data.csv').next();
  var csvData = Utilities.parseCsv(file.getBlob().getDataAsString());
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}

Fetch and import CSV file from an external website

function importCSVFromWeb() {
  // Provide the full URL of the CSV file.
  var csvUrl = 'https://ctrlq.org/data.csv';
  var csvContent = UrlFetchApp.fetch(csvUrl).getContentText();
  var csvData = Utilities.parseCsv(csvContent);

  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}

This method can however not be used for importing CSV files hosted on FTP servers as the UrlFetchApp service of Google Scripts doesn’t allow connecting to FTP servers, yet.

相关文章

How to Link Postal Addresses to Google Maps in Google Sheets

How to Link Postal Addresses to Google Maps in Google Sheets

Bob Canning writes: I have a Google Spreadsheet with postal addresses in column A. Each week, a real...

How to Email Google Sheets Automatically on a Recurring Schedule

How to Email Google Sheets Automatically on a Recurring Schedule

The Email Spreadsheets add-on for Google Sheets can save office workers a ton of time by automating...

Google Maps Formulas for Google Sheets

Google Maps Formulas for Google Sheets

You can bring the power of Google Maps to your Google Sheets using simple formulas with no coding. Y...

Convert Google Docs and Google Sheets with Apps Script

You can easily convert any Google Spreadsheet or Google Document in your Google Drive to other forma...

Send WhatsApp Messages with a Google Sheets Function

Send WhatsApp Messages with a Google Sheets Function

In a previous tutorial, you learned how to send WhatsApp messages from Google Sheets using the offic...

How to Play an MP3 File in Google Sheets

How to Play an MP3 File in Google Sheets

You can put the link of any MP3 audio file in Google Sheets but when you click the file link, the au...