-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset-today-stock-price.gs
40 lines (32 loc) · 1.1 KB
/
set-today-stock-price.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
const getUrl = (symbol) => `https://marketdata.set.or.th/mkt/stockchartdata.do?symbol=${symbol}&time=${+new Date()}`
const stocks = ["E1VFVN3001"];
function updatePriceFor(symbol) {
var response = UrlFetchApp.fetch(getUrl(symbol));
var data = Utilities.parseCsv(response);
var dayEnd = data[data.length-1];
const [d, m, y] = dayEnd[0].split(' ')[0].split('/');
const date = new Date((+y), (+m)-1, +d);
const closedPrice = dayEnd[2];
if (!closedPrice) {
console.log("No data for date", date);
return;
}
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(symbol);
var lastRowNum = sheet.getLastRow();
var lastDate = sheet.getRange(`A${lastRowNum}`).getValue();
if (date.getTime() !== lastDate.getTime()) {
console.log(`Updating data for ${symbol} @ ${date}`);
var nextCell = sheet.getRange(`A${lastRowNum + 1}:B${lastRowNum + 1}`);
nextCell.setValues([
[date, closedPrice]
]);
} else {
console.log(`Data for ${symbol} @ ${date} already exist.`);
}
}
function main() {
for(let symbol of stocks) {
updatePriceFor(symbol)
}
}