I believe your goal as follows.
- You want to retrieve the number of comments of the specific sheet in a Google Spreadsheet using Google Apps Script.
Issue and workaround:
Unfortunately, in the current stage, there are no methods for directly retrieving the number of comments from the specific sheet of Google Spreadsheet in Spreadsheet service, Sheets API and Drive API. This has also mentioned at this thread. So in this answer, I would like to propose a workaround for retrieving the number of comments from the specific sheet using Google Apps Script.
In this workaround, I use Microsoft Excel Data converted from Google Spreadsheet. Even when Google Spreadsheet is converted to Microsoft Excel Data, the comments are not removed. This workaround uses this situation. So I would like to propose to retrieve the number of comments from the specific sheet using Excel Data converted from Spreadsheet. The flow of this workaround is as follows.
- Convert Google Spreadsheet to Microsoft Excel (XLSX data) using Drive API.
- Parse XLSX data using Google Apps Script.
- When the converted XLSX data is unzipped, the data can be analyzed as the XML data. Fortunately, at Microsoft Docs, the detail specification is published as Open XML. So in this case, XLSX data can be analyzed using XmlService of Google Apps Script.
- Retrieve the number of comments from the analyzed XLSX data using the sheet name.
When above flow is converted to the script, I thought that it is a bit complicated. So I created it as a Google Apps Script library (DocsServiceApp (Author is tanaike.)).
Sample script:
Before you use this script, please install DocsServiceApp of the Google Apps Script library.
function myFunction() {
const sheetName = "Sheet1"; // Please set the sheet name.
const spreadsheetId = SpreadsheetApp.getActiveSpreadsheet().getId(); // Please set Spreadsheet ID.
const res = DocsServiceApp.openBySpreadsheetId(spreadsheetId).getSheetByName(sheetName).getComments();
console.log(res.length);
}
- In this script, the number of comments of "Sheet1" on the active Spreadsheet is retrieved.
- When you see the log of
console.log(res)
, you can also confirm the comments including the cell coordinates and authors in the specific sheet.
Note:
- Please use this script with enabling V8.
- If an error related to Drive API occurs, please enable Drive API at Advanced Google services. Ref
References: