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

How to Convert Microsoft Excel to Google Spreadsheet Format with Apps Script

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

If your colleagues have been emailing you Microsoft Excel spreadsheets in xls or xlsx format, here’s a little snippet that will help you convert those Excel sheets into native Google Spreadsheet format using the Advanced Drive API service of Google Apps Script.

function convertExceltoGoogleSpreadsheet(fileName) {
  try {
    // Written by Amit Agarwal
    // www.ctrlq.org

    fileName = fileName || 'microsoft-excel.xlsx';

    var excelFile = DriveApp.getFilesByName(fileName).next();
    var fileId = excelFile.getId();
    var folderId = Drive.Files.get(fileId).parents[0].id;
    var blob = excelFile.getBlob();
    var resource = {
      title: excelFile.getName(),
      mimeType: MimeType.GOOGLE_SHEETS,
      parents: [{ id: folderId }],
    };

    Drive.Files.insert(resource, blob);
  } catch (f) {
    Logger.log(f.toString());
  }
}

The script finds the existing Excel workbook by name in your Google Drive, gets the blob of the file and creates a new file of Google Sheets mimetype (application/vnd.google-apps.spreadsheet) with the blob.

You do need to enable the Google Drive API under Resources > Advanced Google Services and also enable the Drive API inside the Google Cloud Platform project associated with your Google Apps Script.

The other option, instead of specifying the mimetype, is to set the argument convert to true and it will automatically convert the source file into corresponding native Google Format at the time of insert it into Google drive.

function convertExceltoGoogleSpreadsheet2(fileName) {
  try {
    fileName = fileName || 'microsoft-excel.xlsx';

    var excelFile = DriveApp.getFilesByName(fileName).next();
    var fileId = excelFile.getId();
    var folderId = Drive.Files.get(fileId).parents[0].id;
    var blob = excelFile.getBlob();
    var resource = {
      title: excelFile.getName().replace(/\.xlsx?/, ''),
      key: fileId,
    };
    Drive.Files.insert(resource, blob, {
      convert: true,
    });
  } catch (f) {
    Logger.log(f.toString());
  }
}

相关文章

How to Export WooCommerce Customers to Google Sheets

How to Export WooCommerce Customers to Google Sheets

If you are running an online store running on WordPress, chances are you are using WooCommerce to ma...

How to Send Email Reminders for Google Forms Automatically

How to Send Email Reminders for Google Forms Automatically

You are using Google Forms to collect registrations for an upcoming event and would like to send ema...

The Best Google Add

The availability of third-party add-ons for Google Docs, Sheets and Google Slides have certainly mad...

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...

How to Use Conditional Formatting in Google Sheets to Highlight Information

How to Use Conditional Formatting in Google Sheets to Highlight Information

Conditional formatting in Google Sheets makes it easy for you to highlight specific cells that meet...

How to Import PayPal Transactions into Google Sheets

How to Import PayPal Transactions into Google Sheets

This tutorial will show you how to import PayPal transactions into Google Sheets with the help of G...