Search code examples
google-docs-api

Is there a way to access the Google Docs Summary from Apps Script?


In March 2022, Google announced autogenerated summaries for some documents, alongside the document outline. I'm wondering if there's a way to access these summaries in gas, e.g. via DocumentApp.


Solution

  • Get summary text from Google Document:

    In the current stage, in order to retrieve the document summary, you can retrieve it using Drive API as follows.

    GET https://www.googleapis.com/drive/v3/files/fileId?fields=description
    

    The sample Google Apps Script is as follows. When you use this, please enable Drive API at Advanced Google services.

    const documentId = "###"; // Please set Document ID.
    const summary = Drive.Files.get(documentId).description;
    console.log(summary)
    
    • When this script is run, the summary text of Google Document can be retrieved.

    • In this case, you can also use Drive service (DriveApp) as follows.

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

    Set summary text to Google Document:

    In order to retrieve the document summary, you can put it using Drive API as follows.

    PATCH https://www.googleapis.com/drive/v3/files/fileId
    
    content-type: application/json
    
    {"description": "sample summary text"}
    

    The sample Google Apps Script is as follows. When you use this, please enable Drive API at Advanced Google services.

    const documentId = "###"; // Please set Document ID.
    const summary = "sample summary";
    Drive.Files.patch({description: summary}, documentId);
    
    • When this script is run, the summary text can be put to Google Document.

    • In this case, you can also use Drive service (DriveApp) as follows.

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

    Note:

    • Unfortunately, in the current stage, I cannot test these scripts for the autogenerated summary in Document. But, I guess that the autogenerated summary might be able to be retrieved by this method.

    References: