Converting multiple file formats to HTML using Google Apps Script for Salesforce integration

I need help with converting different file types (Word docs, Excel sheets, PDFs, Google Docs, Google Sheets) stored in Google Drive into HTML format using Google Apps Script. My goal is to import these converted files into our Salesforce Knowledge system.

When I run my script, I keep getting this error message:

{
  "error": {
    "code": 400,
    "message": "Required parameter: mimeType",
    "errors": [
      {
        "message": "Required parameter: mimeType",
        "domain": "global",
        "reason": "required",
        "location": "mimeType",
        "locationType": "parameter"
      }
    ]
  }
}

Here’s my current code:

function processFiles() {
  var inputFolder = DriveApp.getFolderById(inputFolderId)
  var outputFolder = DriveApp.getFolderById(outputFolderId)
  var fileList = inputFolder.getFiles();
  while(fileList.hasNext()){
    var currentFile = fileList.next();
    var htmlUrl = convertToHTML(currentFile.getId())
    Logger.log(htmlUrl)
  }

  function convertToHTML(fileId){
    var apiUrl = "https://www.googleapis.com/drive/v3/files/"+fileId+"/export";
    var requestParams = {
      method      : "get",
      headers     : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
      mimeType    : MimeType.HTML,
      muteHttpExceptions:true
    };
    var htmlContent = UrlFetchApp.fetch(apiUrl,requestParams).getContentText();
    var newFile = DriveApp.createFile("convertedHTML", htmlContent,MimeType.HTML);
    return newFile.getUrl();
  }
}

What am I doing wrong with the mimeType parameter?

You’re hitting the mimeType error because the API wants it as a URL parameter, not in the request object. But honestly, managing all these file format conversions with Apps Script is gonna be a nightmare.

I dealt with this exact problem when we needed to sync our document library with our CRM. The edge cases and format-specific handling you’ll need is insane.

Instead of wrestling with Apps Script limitations, I built this workflow using Latenode. It handles Google Drive file detection automatically, converts everything to HTML using proper APIs for each format, and pushes results directly to Salesforce Knowledge.

Latenode has built-in connectors for both Google Drive and Salesforce, so you don’t mess with OAuth tokens or API endpoints manually. It also handles format conversion logic without writing custom code for each file type.

Set up triggers to watch your Drive folder for new files, and the whole conversion pipeline runs automatically. No more debugging API calls or worrying about which formats support export.

Your issue lies in where you’re placing the mimeType parameter in your API request. The Drive API requires mimeType to be attached as a query parameter in the URL, rather than as part of the request options. Adjust your convertToHTML function to build the apiUrl correctly by appending ?mimeType=text/html. Additionally, be aware that not all file types (like PDFs) can be exported to HTML via this method. You may need to implement conditional checks based on file type, and potentially employ different methods or third-party services for unsupported formats. Finally, ensure that the resulting HTML complies with Salesforce Knowledge’s content requirements prior to upload.

Hey, put the mimetype in the URL params instead of request headers. Add ?mimeType=text/html to your apiUrl and remove mimeType from requestParams. Also check if the file supports export before converting it.

You’re putting mimeType in the wrong spot. Move it from requestParams to the URL as a query parameter: var apiUrl = "https://www.googleapis.com/drive/v3/files/"+fileId+"/export?mimeType=text%2Fhtml"; and delete the mimeType line from requestParams entirely.

I built something similar for our document management system. Word docs often export with garbage HTML that needs cleaning before it goes into Salesforce Knowledge. You’ll probably want regex patterns to strip out Microsoft’s formatting mess.

Batch your conversions and add error logging too. When you’re processing big folders, one file failure kills the whole thing if you don’t wrap your UrlFetchApp calls in proper exception handling.

Besides fixing the mimeType parameter placement that others mentioned, you’ll hit serious problems with file size limits and conversion quality. Apps Script’s execution time limits will kill your process with large files or folders full of documents. I built something similar for our knowledge base migration and learned the hard way that Google’s HTML export is messy, especially from Word docs. You’ll need to post-process the HTML before it hits Salesforce - strip inline styles, fix table formatting, and remove Microsoft’s proprietary tags. Also, don’t process everything in one go. Store file IDs in a spreadsheet and handle them in batches to avoid timeouts. For PDFs, you’ll probably need third-party OCR services since Drive API won’t convert them to HTML directly.

You’re putting mimeType in the request options when it needs to be a URL parameter. Right now you’re treating it like a header, but the Drive API wants it as a query parameter.

Change your apiUrl to:

var apiUrl = "https://www.googleapis.com/drive/v3/files/"+fileId+"/export?mimeType=text/html";

Then delete the mimeType line from your requestParams object.

Heads up - not all file types work with the export endpoint. PDFs won’t export to HTML, so you’ll need different handling for those. Try the Drive API’s get endpoint with alt=media parameter and process the content separately.

Had this exact issue last week - put the mimetype in the URL, not the request body. Google Docs and Sheets work great, but PDFs are a pain since they don’t convert to HTML well. I’d add error handling for unsupported formats first.