我需要按作者计算 Google 表中的评论。
我使用了 Google Apps 脚本库和 Drive API,我达到了以下代码:
function countCommentsInSheet() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ssId = ss.getId();
const sheet = ss.getActiveSheet();
const res = DocsServiceApp.openBySpreadsheetId(ssId).getSheetByName(sheet.getSheetName()).getComments();
const contar = res.length;
Logger.log(res);
Logger.log(contar);
}
我得到这个结果:
2:12:40 PM Notice Execution started
2:12:46 PM Info [{comment=[{comment=second comment
, user=Manuel Ocampo}], range={row=2.0, a1Notation=B2, col=2.0}}, {comment=[{user=Manuel Ocampo, comment=first comment
}], range={row=1.0, col=1.0, a1Notation=A1}}]
2:12:46 PM Info 2.0
2:12:42 PM Notice Execution completed
现在我需要按评论作者过滤结果,你能帮我完成代码并按作者过滤结果吗?
您可以使用
for()
循环访问 object
的元素并识别对工作表发表评论的用户。然后,您可以使用另一个 for()
循环来计算每个用户的评论数。
让我知道此代码是否适合您:
function countCommentsInSheet() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ssId = ss.getId();
const sheet = ss.getActiveSheet();
const res = DocsServiceApp.openBySpreadsheetId(ssId).getSheetByName(sheet.getSheetName()).getComments();
const contar = res.length;
var users = [];
for (var x in res) {
//This loop iterates through all of the comments captured and stores the users in an array.
if (users.indexOf(res[x].comment[0].user) == -1) users.push(res[x].comment[0].user);
}
for (var y in users) {
//This loop then counts and logs the number of comments per user.
var commentCount = 0;
for (var z in res) {
if (res[z].comment[0].user == users[y]) commentCount++
}
Logger.log(`${users[y]}: ${commentCount}`)
}
}