-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexport-comments.gs
71 lines (54 loc) · 2.26 KB
/
export-comments.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
function menu_exportComments() {
if (FbSdkIsLoggedIn()) {
promptForPostId();
} else {
showDialogForFbLoginWithNextAction('promptForPostId');
}
}
function promptForPostId() {
var ui = SpreadsheetApp.getUi();
var promptResponse = ui.prompt('Post ID (leave blank for current sheet):');
if (promptResponse.getSelectedButton() !== ui.Button.CLOSE) {
exportCommentsFromPostId(promptResponse.getResponseText());
}
}
////////////////////////////////////////////////////////////////////////////////
// Simon's code...
////////////////////////////////////////////////////////////////////////////////
function exportCommentsFromPostId(id) {
var ui = SpreadsheetApp.getUi();
var spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
id = id ? id : spreadSheet.getActiveSheet().getName();
//ui.alert(id);return;
var sheet = spreadSheet.getSheetByName(id);
if (sheet === null) {
sheet = spreadSheet.insertSheet(id);
}
sheet.clear();
sheet.appendRow(['Name', 'Comment', 'Link to Comment', 'Likes', 'Created Time']);
var limit = 500; // Did a few experiments to find ~800 to be a good maximum page size
getComments('/'+id+'/comments?fields=created_time,from,message,id,like_count&order=reverse_chronological&limit='+limit);
function getComments(endpoint) {
//var spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
//var sheet = spreadSheet.getSheetByName(id);
endpoint = endpoint.replace(/https?:\/\/graph\.facebook\.com\/v\d+\.\d+/, '');
//ui.alert(endpoint);
var resp = FB.api(endpoint);
//Logger.log(resp);return;
var nextUrl = resp.paging.next;
var comments = resp.data;
//// Slow loop insert (deprecated)
//comments.forEach(function(comment){
// sheet.appendRow([comment.from.name, comment.message, 'https://www.facebook.com/' + comment.id, comment.created_time]);
//});
// Fast range insert
var range = sheet.getRange(sheet.getLastRow() + 1, 1, comments.length, 5); // Last param must match number of columns
range.setValues(comments.map(function(comment) {
return [comment.from.name, comment.message, 'https://www.facebook.com/' + comment.id, comment.like_count, comment.created_time];
}));
// Page through results
if (nextUrl) {
getComments(nextUrl);
}
}
}