如何将 Google Docs 文件转换为 Excel 文件 (XLSX) [英] How to convert a Google Docs-File to an Excel-File (XLSX)

查看:62
本文介绍了如何将 Google Docs 文件转换为 Excel 文件 (XLSX)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

图像显示了更新的代码.

The image shows the code who is updated.

var "xlsFile" 未定义,为什么?如何使用 (GoogleDocs) ScriptEditor 将 Googledocs 文件转换为 Excel 文件

The var "xlsFile" is undefined, why? How can I convert the Googledocs-File to an Excel-File with (GoogleDocs) ScriptEditor

 function googleOAuth_ (name, scope) {
       var oAuthConfig = UrlFetchApp.addOAuthService(name);
       oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?     scope="+scope);
       oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
       oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
       oAuthConfig.setConsumerKey('anonymous');
       oAuthConfig.setConsumerSecret('anonymous');
       return {oAuthServiceName:name, oAuthUseToken:"always"};
    }

 function test(){
      var id = '#'
      exportToXls(id)
 }

    function exportToXls(id){
       var mute =  {muteHttpExceptions: true };
       var name = DriveApp.getFileById(id).getName()
       var url = 'https://docs.google.com/feeds/';
       var doc = UrlFetchApp.fetch(url+'download/spreadsheets/Export?key='+id+'&exportFormat=xls',     mute).getBlob()
       var xlsfile = DocsList.createFile(doc).rename(name+'.xlsx')
    }

推荐答案

使用 Drive API,我们可以获得比 DriveApp 方法更多的关于文件的信息.查看文件数据,尤其是exportLinks.这些链接包含让我们获得 XLS 文件的魔法.(为了好玩,在 file 分配后放置一个断点,并检查您必须使用的信息.)

Using the Drive API, we can get more information about files than is available through the DriveApp methods. Check out the file data, especially exportLinks. Those links contain the magic that will let us get an XLS file. (For fun, put a breakpoint after file is assigned, and check what information you have to play with.)

此脚本使用 高级云端硬盘服务必须启用.this gist中提供了更完整的版本,带有错误检查功能.

This script uses the Advanced Drive Service, which must be enabled. A more complete version, with error checking, is available in this gist.

/**
 * Downloads spreadsheet with given file id as an Excel file.
 * Uses Advanced Drive Service, which must be enabled. * Throws if error encountered.
 *
 * @param {String}   fileId       File ID of Sheets file on Drive.
 */
function downloadXLS(fileId) {
  var file = Drive.Files.get(fileId);
  var url = file.exportLinks[MimeType.MICROSOFT_EXCEL];

  var options = {
    headers: {
      Authorization:"Bearer "+ScriptApp.getOAuthToken()
    },
    muteHttpExceptions : true        /// Get failure results
  }

  var response = UrlFetchApp.fetch(url, options);
  var status = response.getResponseCode();
  var result = response.getContentText();
  if (status != 200) {
    // Get additional error message info, depending on format
    if (result.toUpperCase().indexOf("<HTML") !== -1) {
      var message = strip_tags(result);
    }
    else if (result.indexOf('errors') != -1) {
      message = JSON.parse(result).error.message;
    }
    throw new Error('Error (' + status + ") " + message );
  }

  var doc = response.getBlob();
  //DocsList.createFile(doc).rename(file.title + '.xlsx') // Deprecated
  DriveApp.createFile(doc).setName(file.title + '.xlsx');
}

这篇关于如何将 Google Docs 文件转换为 Excel 文件 (XLSX)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆