使用Google云端硬盘的REST API创建新的电子表格

时间:2015-09-27 02:11:21

标签: javascript google-chrome-extension google-drive-api

我正在构建Chrome扩展程序,并希望用户能够创建新的电子表格。根据{{​​3}}。以下是我尝试这样做的方法:

Sweeper.GSheets.prototype.createSpreadsheet = function(title) {
  var data, headers, callback, delimiter, boundary;
  callback = function(status, responseText){
    console.log(responseText);
  };

  title = title + ' --CD.xls';
  delimiter = '\r\n';
  boundary = '--Sweeper';

  data = boundary + delimiter + 'Content-Type: application/json; charset=UTF-8' + delimiter + delimiter + '{' + delimiter + '"title"' + ': ' + '"' + title + '"' + delimiter + '}' + delimiter + delimiter + boundary + delimiter + 'Content-Type: application/vnd.google-apps.spreadsheet' + delimiter + ' ' + delimiter + boundary + '--';  

  headers = { 'Content-Type': 'multipart/related; boundary="Sweeper"', "Convert": "true" };

  this.makeRequest('post','https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart', callback, data, headers );


};


Sweeper.GSheets.prototype.makeRequest = function(method, url, callback, opt_data, opt_headers) {
  setTimeout(function(){
    var data = opt_data || null;
    var headers = opt_headers || {};

    var xhr = new XMLHttpRequest();
    xhr.open(method, url, true);

    // Include common headers (auth and version) and add rest. 
    xhr.setRequestHeader('Authorization', 'Bearer ' + State.authToken());

    for (var key in headers) {
      xhr.setRequestHeader(key, headers[key]);
    }

    xhr.onload = function(e) {
      this.lastResponse = e.srcElement;
      callback(e.srcElement.status, e.srcElement.responseText);
    }.bind(this);

    xhr.onerror = function(e) {
      Sweeper.log(this, this.status, this.response,
          this.getAllResponseHeaders());
    };
    xhr.send(data);
  }.bind(this), 500);
};

这实际上会创建一个Google云端硬盘识别为电子表格的文档,但是,我无法查看该表格。当我点击它时,我收到一般错误。

有没有人知道如何发出此请求以便创建一个空的电子表格?

0 个答案:

没有答案