如何按doc id下载文档

时间:2012-09-25 07:02:43

标签: google-apps-script

function getDocuments(user){
  var scope = 'https://docs.google.com/feeds/';
  //oAuth
  user="user@yourdomain.com"
  var fetchArgs = googleOAuth_('docs', scope);
  var url = scope + user+'/private/full?v=3&alt=json';
  var urlFetch = UrlFetchApp.fetch(url, fetchArgs);
  var json=Utilities.jsonParse(urlFetch.getContentText())
  var entry = json.feed.entry;
  var docs = [];
  for(var i in entry){
    var tempDoc = {};
    for(var j in entry[i]){
      tempDoc.id = entry[i].id.$t.split('%3A')[1];
    }
    docs.push(tempDoc);
  }

  var url='https://docs.google.com/feeds/download/documents/Export?docID='+docs[0]+'&exportFormat=docs'
UrlFetchApp.fetch(url,fetchArgs)

}

//--------------------------------------------------------------------------------------
//Google oAuth
//Used by getDocuments(user)
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(consumerKey);
  oAuthConfig.setConsumerSecret(consumerSecret);
  return {oAuthServiceName:name, oAuthUseToken:"always",method:"GET"};
}

我正在尝试使用文档ID下载文档的代码。我有一系列文档ID作为docs&我使用导出格式作为文档....所以请对此代码给出一些建议..... 当我运行此代码时,它会给出错误: 错误302.暂时移动..

1 个答案:

答案 0 :(得分:0)

试试这个:

 for(var i in entry){
    var tempDoc = {};
    for(var j in entry[i]){
      tempDoc.id = entry[i].id.$t.split('%3A')[1];
    }
//now your splitting your string nicely but you also want to use that
    docs.push(tempDoc.id);
  }

  var url='https://docs.google.com/feeds/download/documents/Export?docID='+docs[0]+'&exportFormat=docs'

//you dont want to use the fetch arguments anymore you have authenticated already
UrlFetchApp.fetch(url)

祝你好运,

Thomas van Latum