Retrieving Summary of Google Document using Google Apps Script

Gists

Retrieving Summary of Google Document using Google Apps Script

This is a sample script for retrieving the summary of Google Document using Google Apps Script. Recently, a blog of Auto-generated Summaries in Google Docs has been posted. I thought that this is very interesting function. I thought that when this function is released, checking each summary of a lot of Google Document will be much useful for simply confirming the document content. And also, I thought that when all summaries can be retrieved using a script, it will be also useful. In this post, I would like to introduce to retrieve the summary of Google Document using Google Apps Script.

1. Retrieve summary from Google Document

In the current stage, the summary of Google Document can be retrieved by Drive service (DriveApp) and Drive API.

When Drive service (DriveApp) is used, the sample script is as follows.

const documentId = "###"; // Please set Document ID.
const summary = DriveApp.getFileById(documentId).getDescription();
console.log(summary);

When Drive API is used, the sample script is as follows. In this case, please enable Drive API at Advanced Google services.

const documentId = "###"; // Please set Document ID.
const summary = Drive.Files.get(documentId).description;
console.log(summary);

As another sample script, when you retrieve all summaries from all Google Document in Google Drive using Google Apps Script, you can also use the following sample script.

function myFunction() {
  const res = [];
  const files = DriveApp.getFilesByType(MimeType.GOOGLE_DOCS);
  while (files.hasNext()) {
    const file = files.next();
    res.push({ title: file.getName(), summary: file.getDescription() || "" });
  }
  console.log(res);
}

2. Create and update summary to Google Document

In the current stage, the summary of Google Document can be retrieved by Drive service (DriveApp) and Drive API.

When Drive service (DriveApp) is used, the sample script is as follows.

const documentId = "###"; // Please set Document ID.
const summary = "sample summary";
DriveApp.getFileById(documentId).setDescription(summary);

When Drive API is used, the sample script is as follows. In this case, please enable Drive API at Advanced Google services.

const documentId = "###"; // Please set Document ID.
const summary = "sample summary";
Drive.Files.patch({ description: summary }, documentId);

Note

Reference

 Share!