-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.bal
46 lines (39 loc) · 1.55 KB
/
main.bal
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
import ballerinax/googleapis.sheets;
configurable string refreshToken = ?;
configurable string clientId = ?;
configurable string clientSecret = ?;
type SalesSummary record {|
string product;
float sales;
|};
final sheets:Client spreadsheetClient = check new ({
auth: {
clientId,
clientSecret,
refreshUrl: sheets:REFRESH_URL,
refreshToken
}
});
function loadToGoogleSheet(string sheetName, string workSheetName, SalesSummary[] salesSummary) returns error? {
// create a new spread sheet
sheets:Spreadsheet spreadsheet = check spreadsheetClient->createSpreadsheet(sheetName);
string spreadSheetId = spreadsheet.spreadsheetId;
// rename the default work sheet name
check spreadsheetClient->renameSheet(spreadSheetId, "Sheet1", workSheetName);
// add legends to the sheet
_ = check spreadsheetClient->appendValue(spreadSheetId, ["Product", "Sales"], {sheetName: workSheetName});
foreach var {product, sales} in salesSummary {
// add sales data to the sheet
_ = check spreadsheetClient->appendValue(spreadSheetId, [product, sales], {sheetName: workSheetName});
}
}
public function main() returns error? {
SalesSummary[] salesSummary = [
{product: "Coffee Maker", sales: 500.50},
{product: "Toaster Oven", sales: 320.75},
{product: "Blender", sales: 180.25},
{product: "Microwave Oven", sales: 420.00},
{product: "Refrigerator", sales: 700.30}
];
check loadToGoogleSheet("Sales Data", "Kitchen Appliences summary", salesSummary);
}