list excel文件的范围和mime类型&从谷歌驱动器以csv格式导出它们

时间:2017-04-17 11:10:50

标签: javascript google-drive-api

以下代码正在列出文件。我只想以csv格式导出文件。此代码提供了所有文件的列表。我只想要excel文件,点击后我想以csv格式下载它们。

 var CLIENT_ID = '***********************';
    var API_KEY = '*************************';

var SCOPES = 'https://www.googleapis.com/auth/drive';

function handleClientLoad() {
    gapi.client.setApiKey(API_KEY);
   // window.setTimeout(checkAuth,1);
}

function checkAuth() {
    var options = {
        client_id: CLIENT_ID,
        scope: SCOPES,
        immediate: true
    };
    gapi.auth.authorize(options, handleAuthResult);
}

function handleAuthResult(authResult) {
    var authorizeButton = document.getElementById('authorize-button');

    if (authResult && !authResult.error) {
        authorizeButton.style.visibility = 'hidden';
        makeApiCall();
    } else {
        authorizeButton.style.visibility = '';
        authorizeButton.onclick = handleAuthClick;
    }
}

function handleAuthClick(event) {
    var options = {
        client_id: CLIENT_ID,
        scope: SCOPES,
        immediate: false
    };
    gapi.auth.authorize(options, handleAuthResult);
    return false;
}

function makeApiCall() {  
    gapi.client.load('drive', 'v2', makeRequest);   
}

function makeRequest() {

    var request = gapi.client.drive.files.list({'maxResults': 5 });
    request.execute(function(resp) {          

        for (i=0; i<resp.items.length; i++) {
            //console.log(resp);
            var titulo = resp.items[i].title;
            var fechaUpd = resp.items[i].modifiedDate;
            var userUpd = resp.items[i].lastModifyingUserName;
            var userEmbed = resp.items[i].embedLink;
            var userAltLink = resp.items[i].alternateLink;
            var fileInfo = document.createElement('li');
            fileInfo.appendChild(document.createTextNode('TITLE: ' + titulo));                
            document.getElementById('content').appendChild(fileInfo);
        }
    });    
}

$(document).ready(function() {
  $('#authorize-button').on('click', handleAuthClick);
  $.getScript('//apis.google.com/js/api.js', function() {
    gapi.load('auth:client', handleClientLoad);
  });
});

我该怎么做。

1 个答案:

答案 0 :(得分:0)

google.picker.ViewId.DOCUMENTS更改为google.picker.ViewId.DOCS

有关详细信息,请参阅https://developers.google.com/picker/docs/reference#ViewId

这将列出所有文件。如果要将其限制为特定的mime类型,则需要创建自己的视图。像

这样的东西
var myExcelView = new google.picker.View(google.picker.ViewId.DOCS);
myExcelView.setMimeTypes("application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); // or whatever mime types you've used
var picker = new google.picker.PickerBuilder()
     .enableFeature(google.picker.Feature.NAV_HIDDEN)
     .enableFeature(google.picker.Feature.MULTISELECT_ENABLED)
     .addView(myExcelView)
     .etc etc