0

So I'm having trouble inserting an anchored comment on a rectangular region of a PDF file using Google Apps Script. I thought I wrote the code completely correct, yet every time I ran the code (below), the comment would come out as unanchored.

I want to know now if my conclusion is correct. It seems to me the issue is that I'm using apps script and apps script uses drive v2. And perhaps I need drive v3 to make my comment.

Compare the documentation for v2 vs v3: https://developers.google.com/drive/api/v2/reference/comments/insert https://developers.google.com/drive/api/v3/reference/comments/create

Looking at the request body table in v2, the comment resource is expected to contain content, and optionally content.type and content.value

Looking at the request body table in v3, the comment resource is expected to contain content, and optionally quotedFileContent.value and anchor

Emphasis on anchor being in v3, not v2.

Does that mean drive v2 absolutely does not support anchored comments? (even though other documentation seems to suggest it does, namely: https://developers.google.com/drive/api/v2/reference/comments/get). Is the documentation self-contradictory, or am I just doing a really bad job of reading documentation?

If I do need v3, exactly how do I get it. I can't find any clear instructions on how to use v3 instead of v2.

Anyway, here's my code:

function myFunction(driveAppFolder) {
  let files = driveAppFolder.getFiles()
    while (files.hasNext()){
      let fileId = files.next().getId()
      let comment = {'content':'Comment Test',
      'anchor':JSON.stringify({
        'r': 'head',
        'a': [
          {
            'rect':
            {
              'x': 20,
              'y': 20,
              'w': 30,
              'h': 30,
            }
          }]
        })
      };
      Drive.Comments.insert(comment, fileId);
    }
}
  • To create a comment in v3 you need to send your request to this url: https://www.googleapis.com/drive/v3/files/fileId/comments and follow these instructions:https://developers.google.com/drive/api/v3/reference/comments/create – Cooper Dec 12 '21 at 00:38
  • How do I send a request to that URL? Sorry, very unfamiliar with that aspect of it. – Baron McDonald Dec 12 '21 at 01:10
  • UrlFetchApp().. – Cooper Dec 12 '21 at 01:13
  • You will also have to enable it in the cloud console – Cooper Dec 12 '21 at 01:14
  • Like this? let requestURL = "https://www.googleapis.com/drive/v3/files/"+fileId+"/comments" UrlFetchApp.fetch(requestURL,comment) – Baron McDonald Dec 12 '21 at 01:17
  • This [link](https://stackoverflow.com/a/67542558/7215091) might be helpful – Cooper Dec 12 '21 at 01:18
  • They can be difficult to use if you have never used them – Cooper Dec 12 '21 at 01:19
  • Is there a good youtube video explaining how to make these HTTP requests/responses/posts via the UrlFetchApp? I see this stuff in the documentation so much, and I have no idea what it is, and I just want someone somewhere to explain it to me clearly. – Baron McDonald Dec 12 '21 at 01:23

1 Answers1

0

As written in the docs,

Note: The Drive API only supports file-level comments on blob files; anchored comments on blob files are not supported.

As written in docs,

A file that contains text or binary content such as images, videos, and PDFs.

So, since PDF is a blob, it's simply not supported. Even in other document file types, the feature is almost useless:

TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • Okay, I guess I didn't really know what a blob file is. – Baron McDonald Dec 12 '21 at 01:07
  • 1
    However, that makes the documentation even more confusing, because it mentions pdfs when describing the "rect" region classifier here: https://developers.google.com/drive/api/v3/manage-comments – Baron McDonald Dec 12 '21 at 01:09