-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.gs
147 lines (124 loc) · 4.58 KB
/
Code.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
*
* Primary function, get things started. The purpose of this script is to return your Dropbox links from https://www.dropbox.com/share/links to a Google Sheet.
*
*/
function primaryFunction(){
// Declare variables
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// Get shared Dropbox files
getSharedDropboxFiles(spreadsheet);
}
/**
*
* Link to Dropbox API and return shared files.
*
* Directions
* 1. Go to https://www.dropbox.com/developers/apps/create?_tk=pilot_lp&_ad=ctabtn1&_camp=create
* 2. Select Dropbox API
* 3. Select Full Dropbox
* 4. Give your App a name (I gave it ryanmcslomo-GoogleAppsScript)
* 5. On the next screen, generate an access token and save it to var dropboxAccessToken on line 30
*
* References
*
* https://www.labnol.org/code/20571-download-web-files-dropbox
* https://www.labnol.org/code/20573-upload-google-drive-files-dropbox
* https://www.dropbox.com/developers/documentation/http/documentation#sharing-list_shared_links
*
* @param spreadsheet {Object} The active spreadsheet object. This is where we'll print the array.
* @param cursor {String} The cursor returned by your last call to list_shared_links, indicates our position in returning links.
*
*/
function getSharedDropboxFiles(spreadsheet, cursor) {
// Pause script to not trigger API limits
Utilities.sleep(3000);
// Declare variables
var linkArray = [];
var parameters = {
// This is optional. You can delete this and return all shared files or add paths to items or you can add paths to folders. For example:
// "path": "/graduate school/ryan's uploads"
// "path": "/graduate school/ryan's uploads/picture.jpg"
// The slashes indicate folder hierarchy. You can also use path ID and a few other tricks.
// More info: https://www.dropbox.com/developers/documentation/http/documentation#sharing-list_shared_links
};
if (cursor){
parameters.cursor = cursor;
}
// Add your Dropbox Access Token
var dropboxAccessToken = 'xXxXxXxXx';
// Set authentication object parameters
var headers = {
"Content-Type": "application/json",
"Authorization": "Bearer " + dropboxAccessToken,
};
// Set option parameters
var options = {
"method": "POST",
"headers": headers,
"muteHttpExceptions": true,
// "payload": JSON.stringify(parameters)
};
// Hit up API
var apiUrl = "https://api.dropboxapi.com/2/sharing/list_shared_links";
try{
var response = UrlFetchApp.fetch(apiUrl, options);
var responseText = response.getContentText();
var responseTextJSON = JSON.parse(responseText);
// Parse JSON response
var links = responseTextJSON.links;
var hasMore = responseTextJSON.has_more;
var cursor = responseTextJSON.cursor;
for (var link = 0; link < links.length; link++){
linkArray.push([links[link].name, links[link].path_lower, links[link].id, links[link][".tag"], links[link].url]);
}
} catch (e) {
console.log(e);
linkArray.push(e);
}
// Print to sheet and continue if there are still more entries
setArrayValuesToSheet(spreadsheet, linkArray, hasMore, cursor);
}
/**
*
* Print array to sheet.
*
* @param sheet {Object} The active spreadsheet object. This is where we'll print the array.
* @param linkArray {Array} The array of returned Dropbox items.
* @param hasMore {Boolean} True if there are more entries, false if we have grabbed them all.
* @param cursor {String} The cursor returned by your last call to list_shared_links, indicates our position in returning links.
*
*/
function setArrayValuesToSheet(spreadsheet, linkArray, hasMore, cursor){
// Add header row if not present
var spreadsheet = spreadsheet || SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getActiveSheet();
var firstCell = sheet.getRange(1, 1).getValue();
var lastRow = sheet.getLastRow();
if (firstCell != 'Name' && linkArray.length > 1) {
var headerRow = ["Name", "Path", "ID", "Tag", "URL"];
linkArray.unshift(headerRow);
}
// Print error message if we got one
if (linkArray.length === 1){
sheet.getRange(lastRow + 1, 1).setValue(linkArray);
}
// Print array to active sheet
sheet.getRange(lastRow + 1, 1, linkArray.length, linkArray[0].length).setValues(linkArray);
SpreadsheetApp.flush();
// If there are more Dropbox files, run function again
if (hasMore){
getSharedDropboxFiles(spreadsheet, cursor);
}
}
/**
*
* Create a menu option for script functions.
*
*/
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Functions')
.addItem('Get Shared Dropbox Files', 'primaryFunction')
.addToUi();
}