Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Last active April 2, 2019 07:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanaikech/915f1034749b8b2f451556167663ea19 to your computer and use it in GitHub Desktop.
Save tanaikech/915f1034749b8b2f451556167663ea19 to your computer and use it in GitHub Desktop.
Creating New Google Docs and Overwriting Existing Google Docs by Text with Node.js without using googleapis

Creating New Google Docs and Overwriting Existing Google Docs by Text with Node.js without using googleapis

There are 2 sample scripts.

  1. Create new Spreadsheet using a text value as CSV data.
  2. Overwrite the existing Google Document using a text value.

When you use these script, please enable Drive API and retrieve your access token.

Create New Spreadsheet using Text Value

const request = require('request');

const textData = "a1, b1, c1, d1, e1"; // This is used as CSV data.
const orgMimeType = "text/csv";
const orgFileName = "sample.csv";
const accessToken = "###"; // Access token
const metadata = {
  name: "convertedSampleCSV",
  mimeType: "application/vnd.google-apps.spreadsheet",
};

const url = "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart";
const boundary = "xxxxxxxxxxx";
var data = "--" + boundary + "\r\n";
  data += "Content-Disposition: form-data; name=\"metadata\"\r\n";
  data += "Content-Type: application/json; charset=UTF-8\r\n\r\n";
  data += JSON.stringify(metadata) + "\r\n";
  data += "--" + boundary + "\r\n";
  data += "Content-Disposition: form-data; name=\"file\"; filename=\"" + orgFileName + "\"\r\n";
  data += "Content-Type: " + orgMimeType + "\r\n\r\n";

var payload = Buffer.concat([
  Buffer.from(data, "utf8"),
  new Buffer(textData, 'binary'),
  Buffer.from("\r\n--" + boundary + "--", "utf8"),
]);

const options = {
  method: 'post',
  url: url,
  headers: {
    "Content-Type": "multipart/related; boundary=" + boundary,
    'Authorization': 'Bearer ' + accessToken,
  },
  body: payload,
};
request(options, (error, response, body) => {
  console.log(body);
});

Overwrite Existing Google Document using Text Value

This can be used when you don't want to update the existing Google Docs without changing the file ID.

const request = require('request');

const textData = "sample text"; // This is used as CSV data.
const orgMimeType = "text/plain";
const orgFileName = "sample.txt";
const googleDocumentFileId = "###"; // File ID of the existing Google Document.
const accessToken = "###"; // Access token
const metadata = {
  mimeType: "application/vnd.google-apps.document",
};

const url = "https://www.googleapis.com/upload/drive/v3/files/" + googleDocumentFileId + "?uploadType=multipart";
const boundary = "xxxxxxxxxxx";
var data = "--" + boundary + "\r\n";
  data += "Content-Disposition: form-data; name=\"metadata\"\r\n";
  data += "Content-Type: application/json; charset=UTF-8\r\n\r\n";
  data += JSON.stringify(metadata) + "\r\n";
  data += "--" + boundary + "\r\n";
  data += "Content-Disposition: form-data; name=\"file\"; filename=\"" + orgFileName + "\"\r\n";
  data += "Content-Type: " + orgMimeType + "\r\n\r\n";

var payload = Buffer.concat([
  Buffer.from(data, "utf8"),
  new Buffer(textData, 'binary'),
  Buffer.from("\r\n--" + boundary + "--", "utf8"),
]);

const options = {
  method: 'patch',
  url: url,
  headers: {
    "Content-Type": "multipart/related; boundary=" + boundary,
    'Authorization': 'Bearer ' + accessToken,
  },
  body: payload,
};
request(options, (error, response, body) => {
  console.log(body);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment