-
Notifications
You must be signed in to change notification settings - Fork 0
/
Edit.gs
200 lines (173 loc) · 6.51 KB
/
Edit.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
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
const sheetID = "<EDIT>";
const sheetName = "Sheet1";
function atEdit(event) {
}
function atChange(event) {
// const sheet = event.source.getActiveSheet();
Logger.log(event.changeType)
if (event.changeType == "INSERT_ROW") {
Logger.log(event.source.getActiveRange().getA1Notation())
syncEntry(event);
}
updateChartBounds();
}
function syncEntry(event) {
const sheet = event.source.getActiveSheet();
const dataRange = sheet.getDataRange();
const regex = /[A-Z]+[0-9]+\:[A-Z]+([0-9]+)/g;
const match = regex.exec(dataRange.getA1Notation());
Logger.log(dataRange.getA1Notation())
if (match == null || match.length < 2) return;
const row = match[1];
if (row == undefined || row == null) return;
const checkbox = sheet.getRange(parseInt(row), 7).insertCheckboxes();
Logger.log(parseInt(row))
let data = sheet.getRange(`A${row}:G${row}`).getValues();
Logger.log(data);
data = data[0];
const date = data[0];
const mass = data[1];
const unit = data[2];
const fatt_kg = data[3];
const fatt_pct = data[4];
const lean = data[5];
// Logger.log(`${date} ${mass} ${unit} ${fatt_kg} ${fatt_pct} ${lean}`);
// December 7, 2021 at 11:24AM
const date_pieces = /(\w+) (\d+), (\d+) at (\d+):(\d+)(AM|PM)/.exec(date);
const day = date_pieces[2];
const month = date_pieces[1].substr(0, 3);
const year = date_pieces[3];
const ampm = date_pieces[6];
const hour = parseInt(date_pieces[4]) + 12 * (ampm == "PM" && date_pieces[4] != "12");
const minute = date_pieces[5];
const dateObj = new Date(`${day} ${month} ${year} ${hour}:${minute}:00`);
// Logger.log(`${day} ${month} ${year} ${hour}:${minute}:00`)
// Logger.log(`${dateObj * 1e6}, ${fatt_pct}`);
updateFat(dateObj.getTime() * 1e6, fatt_pct);
checkbox.check();
sheet.getRange(parseInt(row), 8).setValue(`${dateObj.getDate()}\/${dateObj.getMonth()+1}\/${dateObj.getFullYear()}`);
}
function syncAll() {
const sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
const dataRange = sheet.getDataRange();
const data = sheet.getRange(dataRange.getA1Notation().replace("A1", "A2")).getValues();
data.forEach((datum, idx) => {
const checkbox = sheet.getRange(2 + idx, 7).insertCheckboxes();
const date = datum[0];
const mass = datum[1];
const unit = datum[2];
const fatt_kg = datum[3];
const fatt_pct = datum[4];
const lean = datum[5];
const synced = datum[6];
const date_pieces = /(\w+) (\d+), (\d+) at (\d+):(\d+)(AM|PM)/.exec(date);
const day = date_pieces[2];
const month = date_pieces[1].substr(0, 3);
const year = date_pieces[3];
const ampm = date_pieces[6];
const hour = parseInt(date_pieces[4]) + 12 * (ampm == "PM" && date_pieces[4] != "12");
const minute = date_pieces[5];
const dateObj = Date.parse(`${day} ${month} ${year} ${hour}:${minute}:00`);
updateFat(dateObj * 1e6, fatt_pct);
checkbox.check();
sheet.getRange(2 + idx, 8).setValue(`${day}\/${month}\/${year} ${hour}:${minute}`);
})
}
function syncLast() {
const sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
const dataRange = sheet.getDataRange();
const regex = /[A-Z]+[0-9]+\:[A-Z]+([0-9]+)/g;
const match = regex.exec(dataRange.getA1Notation());
Logger.log(dataRange.getA1Notation())
if (match == null || match.length < 2) return;
const row = match[1];
if (row == undefined || row == null) return;
const checkbox = sheet.getRange(parseInt(row), 7).insertCheckboxes();
Logger.log(parseInt(row))
let data = sheet.getRange(`A${row}:G${row}`).getValues();
Logger.log(data);
data = data[0];
const date = data[0];
const mass = data[1];
const unit = data[2];
const fatt_kg = data[3];
const fatt_pct = data[4];
const lean = data[5];
// Logger.log(`${date} ${mass} ${unit} ${fatt_kg} ${fatt_pct} ${lean}`);
// December 7, 2021 at 11:24AM
const date_pieces = /(\w+) (\d+), (\d+) at (\d+):(\d+)(AM|PM)/.exec(date);
const day = date_pieces[2];
const month = date_pieces[1].substr(0, 3);
const year = date_pieces[3];
const ampm = date_pieces[6];
const hour = parseInt(date_pieces[4]) + 12 * (ampm == "PM" && date_pieces[4] != "12");
const minute = date_pieces[5];
const dateObj = Date.parse(`${day} ${month} ${year} ${hour}:${minute}:00`);
// Logger.log(`${day} ${month} ${year} ${hour}:${minute}:00`)
// Logger.log(`${dateObj * 1e6}, ${fatt_pct}`);
updateFat(dateObj * 1e6, fatt_pct);
checkbox.check();
sheet.getRange(parseInt(row), 8).setValue(`${day}\/${month}\/${year} ${hour}:${minute}`);
}
function syncUnchecked() {
const sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
const dataRange = sheet.getDataRange();
const data = sheet.getRange(dataRange.getA1Notation().replace("A1", "A2")).getValues();
data.forEach((datum, idx) => {
const checkbox = sheet.getRange(2 + idx, 7).insertCheckboxes()
if (checkbox.isChecked()) {
return;
}
const date = datum[0];
const mass = datum[1];
const unit = datum[2];
const fatt_kg = datum[3];
const fatt_pct = datum[4];
const lean = datum[5];
const synced = datum[6];
const date_pieces = /(\w+) (\d+), (\d+) at (\d+):(\d+)(AM|PM)/.exec(date);
const day = date_pieces[2];
const month = date_pieces[1].substr(0, 3);
const year = date_pieces[3];
const ampm = date_pieces[6];
const hour = parseInt(date_pieces[4]) + 12 * (ampm == "PM" && date_pieces[4] != "12");
const minute = date_pieces[5];
const dateObj = new Date(`${day} ${month} ${year} ${hour}:${minute}:00`);
updateFat(dateObj.getTime() * 1e6, fatt_pct);
checkbox.check();
sheet.getRange(2 + idx, 8).setValue(`${dateObj.getDate()}\/${dateObj.getMonth()+1}\/${dateObj.getFullYear()}`);
})
}
function updateChartBounds() {
const sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
const highest = Math.ceil(sheet.getRange("J2").getValues()[0][0]);
const lowest = Math.floor(sheet.getRange("L2").getValues()[0][0]);
let chart = sheet.getCharts()[0];
chart = chart.modify()
.setOption('vAxes', {
0: {
format: 'none',
viewWindow: {
max: highest,
min: lowest
}
}
})
.setOption('trendlines', {
0: {
type: 'polynomial',
degree: 4,
lineWidth: 2,
opacity: 0.8,
color: '#4285f4',
visibleInLegend: false
}
})
.build();
sheet.updateChart(chart);
}
// function customSync() {
// const date = 1639009640000;
// const value = "20.33";
// updateFat(date * 1e6, value);
// }