-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCode.js
114 lines (93 loc) · 3.27 KB
/
Code.js
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
var ss = SpreadsheetApp.getActiveSheet();
function onOpen() {
SpreadsheetApp.getUi()
.createMenu( 'Sketchfab' )
.addItem( 'Upload', 'showUploadSidebar' )
.addToUi();
}
function showUploadSidebar() {
var style = HtmlService.createHtmlOutputFromFile( 'stylesheet' ),
script = HtmlService.createHtmlOutputFromFile( 'javascript' ),
html = HtmlService.createHtmlOutputFromFile( 'upload' )
.setSandboxMode( HtmlService.SandboxMode.IFRAME )
.setTitle( 'Upload' )
.setWidth( 300 )
.append( style.getContent() )
.append( script.getContent() );
SpreadsheetApp.getUi()
.showSidebar( html );
}
function populateSheet( modelsArr, rowCount, columnCount ) {
Logger.log( 'model count: ' + ( rowCount - 1 ) );
// Ranges
var fullRange = ss.getRange( 1, 1, rowCount, columnCount ),
licRange = ss.getRange( 'G2:G' + rowCount ),
catRange = ss.getRange( 'H2:H' + rowCount ),
// Download
licSlugs = [
'CC Attribution',
'CC Attribution-ShareAlike',
'CC Attribution-NoDerivs',
'CC Attribution-NonCommercial',
'CC Attribution-NonCommercial-ShareAlike',
'CC Attribution-NonCommercial-NoDerivs',
'CC0 Public Domain'
],
licRule,
// Categories
cats = getCategories(),
catSlugs = [],
catRule;
// Populate sheet
fullRange.setValues( modelsArr );
// Build data validation rules
for ( var row = 2; row < rowCount; row++ ) {
var nameRange = ss.getRange( 'B' + row ),
desRange = ss.getRange( 'C' + row ),
pwRange = ss.getRange( 'F' + row ),
nameRule = SpreadsheetApp.newDataValidation()
.requireFormulaSatisfied( '=LTE(LEN(B' + row + '), 48)' )
.setHelpText( 'Model name must be 48 characters or less.' )
.build(),
desRule = SpreadsheetApp.newDataValidation()
.requireFormulaSatisfied( '=LTE(LEN(C' + row + '), 1024)' )
.setHelpText( 'Model description must be 256 characters or less.' )
.build(),
pwRule = SpreadsheetApp.newDataValidation()
.requireFormulaSatisfied( '=LTE(LEN(F' + row + '), 64)' )
.setHelpText( 'Model password must be 64 characters or less.' )
.build();
nameRange.setDataValidation( nameRule );
desRange.setDataValidation( desRule );
pwRange.setDataValidation( pwRule );
}
for ( var cat = 0; cat < cats.length; cat++ ) {
catSlugs.push( cats[ cat ].slug );
}
licRule = SpreadsheetApp.newDataValidation()
.requireValueInList( licSlugs, true )
.build();
licRange.setDataValidation( licRule );
catRule = SpreadsheetApp.newDataValidation()
.requireValueInList( catSlugs.sort(), true )
.build();
catRange.setDataValidation( catRule );
// Pass categories back to client
return cats;
}
function getMetaData() {
var range = ss.getRange( 1, 1, ss.getMaxRows(), ss.getMaxColumns() ),
values = range.getValues();
Logger.log( 'getMetaData ran: ');
Logger.log( values );
return values;
}
function clearSheet() {
var range = ss.getRange( 1, 1, ss.getMaxRows(), ss.getMaxColumns() );
range.clear().clearDataValidations();
}
function getCategories() {
var catUrl = 'https://api.sketchfab.com/v3/categories',
catResponse = UrlFetchApp.fetch( catUrl );
return JSON.parse( catResponse.getContentText() ).results;
}