最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Exporting Google Drive files (.docx, .xlsx, .pdf, Google Docs, Google Sheets) as .html files with Google Apps Script - Stack Ove

programmeradmin4浏览0评论

I am trying to export various files (.docx, .xlsx, .pdf, Google Docs, Google Sheets) in a Google Drive folder as .html files with Google Apps Script to load into a Salesforce Knowledge Base. The code below results in a text file with the error:

{
  "error": {
    "code": 400,
    "message": "Required parameter: mimeType",
    "errors": [
      {
        "message": "Required parameter: mimeType",
        "domain": "global",
        "reason": "required",
        "location": "mimeType",
        "locationType": "parameter"
      }
    ]
  }
}
function downloadFile() {
  var sourcefolder = DriveApp.getFolderById(sourceFolderId)
  var destinationfolder = DriveApp.getFolderById(destinationFolderId)
  var files = sourcefolder.getFiles();
  while(files.hasNext()){
    var file = files.next();
    var url = exportAsHTML(file.getId())
    Logger.log(url)
  }

  function exportAsHTML(documentId){
    var url = "/"+documentId+"/export";
    var param = {
      method      : "get",
      headers     : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
      mimeType    : MimeType.HTML,
      muteHttpExceptions:true
    };
    var html = UrlFetchApp.fetch(url,param).getContentText();
    var file = DriveApp.createFile("myExportedHTML", html,MimeType.HTML);
    return file.getUrl();
  }
}

I am trying to export various files (.docx, .xlsx, .pdf, Google Docs, Google Sheets) in a Google Drive folder as .html files with Google Apps Script to load into a Salesforce Knowledge Base. The code below results in a text file with the error:

{
  "error": {
    "code": 400,
    "message": "Required parameter: mimeType",
    "errors": [
      {
        "message": "Required parameter: mimeType",
        "domain": "global",
        "reason": "required",
        "location": "mimeType",
        "locationType": "parameter"
      }
    ]
  }
}
function downloadFile() {
  var sourcefolder = DriveApp.getFolderById(sourceFolderId)
  var destinationfolder = DriveApp.getFolderById(destinationFolderId)
  var files = sourcefolder.getFiles();
  while(files.hasNext()){
    var file = files.next();
    var url = exportAsHTML(file.getId())
    Logger.log(url)
  }

  function exportAsHTML(documentId){
    var url = "https://www.googleapis/drive/v3/files/"+documentId+"/export";
    var param = {
      method      : "get",
      headers     : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
      mimeType    : MimeType.HTML,
      muteHttpExceptions:true
    };
    var html = UrlFetchApp.fetch(url,param).getContentText();
    var file = DriveApp.createFile("myExportedHTML", html,MimeType.HTML);
    return file.getUrl();
  }
}
Share Improve this question edited Feb 20 at 20:10 Wicket 38.4k9 gold badges78 silver badges192 bronze badges asked Feb 20 at 18:15 H.I. McDunnoughH.I. McDunnough 112 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

The mime type should be sent as a query parameter:

var url = `https://www.googleapis/drive/v3/files/${documentId}/export?mimeType=${MimeType.HTML}`;

For non-Google docs files like docx, xlsx, you can try converting them to Google docs. See https://developers.google/drive/api/guides/manage-uploads#import-docs

There is no general "turn key" to convert files stored in Google Drive to HTML.

For Google Editors files (Google Docs, Google Sheets), it's possible to "convert" them using the built-in feature to export the file as HTML.

For file formats that can be converted to Google Editor files, you might want first to convert them to the corresponding Google Editor file format.

For non-Google Editors files, you should find a way to parse the content and then save that content as HTML. This implies creating a string with the HTML structure using the corresponding HTML tags to each part of the content. You should decide how the format will be handled, i.e., using inline styles by creating one or more CSS files, among other options.

Regarding using the Google Drive API V3 end-point to export Google Editors files, you might want to check that the MimeType is one of the Google Editors formats before fetching the export as an HTML end-point.

Reference

  • Download and export files

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论