async function main(workbook: ExcelScript.Workbook) {
const sheetName = "CafeF-PriceHistory";
let sheet = workbook.getWorksheet(sheetName);
if (!sheet) {
sheet = workbook.addWorksheet(sheetName);
}
const result = await getPriceHistory("VNINDEX", "8/1/2024", "8/31/2024", 500);
const range = sheet.getRangeByIndexes(0, 0, result.length, 11);
range.setValues(result);
sheet.getRange("A1:K1").getFormat().getFont().setBold(true);
}
async function getPriceHistory(symbol: string, StartDate: string, EndDate: string, PageSize = 20): Promise<(string|number|boolean)[][]> {
let uri = `https://s.cafef.vn/Ajax/PageNew/DataHistory/PriceHistory.ashx?Symbol=${symbol}&StartDate=${StartDate}&EndDate=${EndDate}&PageIndex=1&PageSize=${PageSize}`;
const response = await fetch(uri, {
headers: {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0",
"Referer": "https://s.cafef.vn/"
}
});
if (response.ok) {
const result: (string|number|boolean)[][] = [];
result.push(["Giá cao nhất", "Giá điều chỉnh", "Giá đóng cửa", "Giá mở cửa", "Giá thấp nhất", "Giá trị khớp lệnh", "Giá trị thỏa thuận", "Khối lượng thỏa thuận", "Khối lượng khớp lệnh", "Ngày", "Thay đổi"]);
const json: PriceHistoryResponse = await response.json();
for (const item of json.Data.Data) {
const itemArray: (string|number|boolean)[] = [item.GiaCaoNhat, item.GiaDieuChinh, item.GiaDongCua, item.GiaMoCua, item.GiaThapNhat, item.GiaTriKhopLenh, item.GtThoaThuan, item.KLThoaThuan, item.KhoiLuongKhopLenh, item.Ngay, item.ThayDoi];
result.push(itemArray);
}
return result;
}
}
interface PriceHistoryResponse {
Data: PriceHistoryData,
Message: string,
Success: boolean
}
interface PriceHistoryData {
Data: Array<PriceHistoryItem>,
TotalCount: number
}
interface PriceHistoryItem {
GiaCaoNhat: number,
GiaDieuChinh: number,
GiaDongCua: number,
GiaMoCua: number,
GiaThapNhat: number,
GiaTriKhopLenh: number,
GtThoaThuan: number,
KLThoaThuan: number,
KhoiLuongKhopLenh: number,
Ngay: string,
ThayDoi: string
}