-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
200 lines (189 loc) · 8 KB
/
index.html
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<!DOCTYPE html>
<html lang="en-GB">
<head>
<title>Ongoing experiment v31</title>
<style>
.row {
display: grid;
gap: 10px;
}
.cell {
grid-row: 1;
text-align: center;
padding-top: 5px;
padding-bottom: 5px;
margin-top: 5px;
margin-bottom: 5px;
}
</style>
<script async defer src="https://apis.google.com/js/api.js" onload="this.onload=function(){};handleClientLoad()"
onreadystatechange="if (this.readyState === 'complete') this.onload()">
</script>
<script>
let params = new URLSearchParams(document.location.search.substring(1));
const pageWidth = params.get("w");
const spreadsheetId = params.get("ssid");
// const spreadsheetId = '1xou6_yJOAtKxzcugUWuE1JpzIhPqI0WMJN05M-U1sJc';
function start() {
// Initializes the client with the API key and the Translate API.
gapi.client.init({
'apiKey': 'AIzaSyBRW6aMCSm4aoo636LnVIExSMYWuow7IV4',
'discoveryDocs': ['https://sheets.googleapis.com/$discovery/rest?version=v4'],
'clientId': '911151820985-ble4mlioiaqb5grec4o0arcc8qc74g5s.apps.googleusercontent.com',
'scope': 'https://www.googleapis.com/auth/spreadsheets.readonly'
}).then(function (response) {
// console.log(response);
gapi.auth2.getAuthInstance().isSignedIn.listen(updateSignInStatus);
updateSignInStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
});
};
function updateSignInStatus(isSignedIn) {
if (isSignedIn) {
execute();
}
}
// Loads the JavaScript client library and invokes `start` afterwards.
function handleClientLoad() {
gapi.load('client:auth2', start);
execute();
}
function execute() {
return gapi.client.sheets.spreadsheets.get({
"spreadsheetId": spreadsheetId,
"includeGridData": true
}).then(function (response) {
// Handle the results here (response.result has the parsed body).
console.log("Response", response.result);
buildHtml(response.result);
},
function (err) {
if (err?.result?.error?.status === 'PERMISSION_DENIED') {
console.log('Document is private, please sign in');
gapi.auth2.getAuthInstance().signIn();
} else {
console.error("Execute error", err);
}
});
}
function calculatedWidth(){
if (parseInt(pageWidth) > 0) {
return parseInt(pageWidth);
} else {
return 'auto';
}
}
var htmlAccumulator = '';
function buildHtml(data) {
let maxRows = 0;
// find the max rows length, used for the CSS grid
for (const rowData in data.sheets[0].data[0].rowData) {
let length = 0;
const maxLength = data.sheets[0].data[0].rowData[rowData].values.length;
for (i = maxLength -1; i >= 0; --i) {
tmpValue = data.sheets[0].data[0].rowData[rowData].values[i];
if (typeof tmpValue?.formattedValue != 'undefined' && tmpValue.formattedValue.trim().length > 0) {
++length;
}
}
if (length > maxRows) {
maxRows = length;
}
}
htmlAccumulator += `<div class='container' style='width:${calculatedWidth()}px'>`;
for (const rowData in data.sheets[0].data[0].rowData) {
// merge same items and increase row width
let newCell = [];
let possition = 0;
let rows = 1;
for (const cell in data.sheets[0].data[0].rowData[rowData].values) {
// remove empty rows
if (cell >= maxRows) {
break;
}
if (typeof newCell[possition - rows] === 'undefined' || newCell[possition - rows].formattedValue != data.sheets[0].data[0].rowData[rowData].values[cell].formattedValue) {
newCell[possition] = {};
// remove empty value and apply colors
if (typeof data.sheets[0].data[0].rowData[rowData].values[cell].formattedValue === 'undefined') {
newCell[possition].formattedValue = '';
newCell[possition].bColor = '';
} else {
newCell[possition].formattedValue = data.sheets[0].data[0].rowData[rowData].values[cell].formattedValue;
newCell[possition].userEnteredFormat = data.sheets[0].data[0].rowData[rowData].values[cell]?.userEnteredFormat;
if(typeof data.sheets[0].data[0].rowData[rowData].values[cell].userEnteredFormat !== 'undefined' &&
typeof data.sheets[0].data[0].rowData[rowData].values[cell].userEnteredFormat.backgroundColor !== 'undefined'
) {
let red = data.sheets[0].data[0].rowData[rowData].values[cell].userEnteredFormat.backgroundColor.red * 255;
let green = data.sheets[0].data[0].rowData[rowData].values[cell].userEnteredFormat.backgroundColor.green * 255;
let blue = data.sheets[0].data[0].rowData[rowData].values[cell].userEnteredFormat.backgroundColor.blue * 255;
newCell[possition].bColor = `;background-color: rgb(${red}, ${green}, ${blue});`;
} else {
newCell[possition].bColor = ';background-color: rgba(80, 80, 80, 0.1);';
}
}
newCell[possition].rowSpawn = 1;
++possition;
rows = 1;
} else {
newCell[possition - rows].rowSpawn = rows + 1;
++rows;
++possition;
}
}
// lines
htmlAccumulator += `<div class='row' style='grid-template-columns: repeat(${maxRows}, 1fr);'>`;
let gridColumnCounter = 1;
for (const cell in newCell) {
if (newCell[cell].userEnteredFormat?.textFormat?.bold == true) {
fontWeight = 'font-weight: bold;';
} else {
fontWeight = 'font-weight: normal;';
}
if (newCell[cell].rowSpawn > 1) {
let columnEnd = gridColumnCounter + newCell[cell].rowSpawn;
htmlAccumulator += `<div class='cell' style='${fontWeight} grid-column: ${gridColumnCounter}/${columnEnd}${newCell[cell].bColor}'>`;
gridColumnCounter = gridColumnCounter + newCell[cell].rowSpawn;
} else {
htmlAccumulator += `<div class='cell' style='${fontWeight} grid-column: ${gridColumnCounter}${newCell[cell].bColor}'>`;
++gridColumnCounter;
}
htmlAccumulator += newCell[cell].formattedValue;
htmlAccumulator += "</div>";
}
htmlAccumulator += "</div>";
// lines
// htmlAccumulator += "<div class='row'>";
// for (const cell in data.sheets[0].data[0].rowData[rowData].values) {
// htmlAccumulator += "<div class='cell'>";
// htmlAccumulator += data.sheets[0].data[0].rowData[rowData].values[cell].formattedValue;
// htmlAccumulator += "</div>";
// // console.log(htmlAccumulator);
// // console.log(data.sheets[0].data[0].rowData[rowData].values[cell].formattedValue);
// }
// htmlAccumulator += "</div>";
}
htmlAccumulator += "</div>";
document.getElementById('results').innerHTML = htmlAccumulator;
// console.log(htmlAccumulator);
}
</script>
</head>
<body>
<div id="results">
<!-- <div class="row">
<div class="cell"></div>
<div class="cell">2-Aug-2021</div>
<div class="cell">9-Aug-2021</div>
<div class="cell">16-Aug-2021</div>
<div class="cell">23-Aug-2021</div>
</div>
<div class="row">
<div class="cell">Person1</div>
<div class="cell">Project Name one</div>
<div class="cell">Project Name one</div>
<div class="cell">Project name two</div>
<div class="cell">Project name two</div>
</div> -->
</div>
<button onclick="handleClientLoad()">execute</button>
</body>
</html>