-
Notifications
You must be signed in to change notification settings - Fork 0
/
Transaction.js
42 lines (36 loc) · 1.36 KB
/
Transaction.js
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
function getTransactionRecommendationFor(summarySheetName){
const ss = SpreadsheetApp.getActiveSpreadsheet();
const summarySheet = ss.getSheetByName(summarySheetName);
const summarySheetData = summarySheet.getDataRange().getValues();
const summarySheetRows = summarySheetData.slice(1);
const diffMap = summarySheetRows.map(row => {
const participant = row[0];
const difference = row[3];
return {
"name": participant,
"weight": difference
};
});
const weightMap = {};
diffMap.forEach(diff => {
weightMap[diff.name] = diff.weight;
});
const recommendation = findTransitiveWeightSequence(weightMap);
let recommendationSheet = ss.getSheetByName(summarySheetName.replace("Summary-", "Recommendation-"));
if (!recommendationSheet) {
recommendationSheet = ss.insertSheet(summarySheetName.replace("Summary-", "Recommendation-"));
} else {
recommendationSheet.clear();
}
const recommendationSheetHeaders = ["From", "To", "Amount"];
const recommendationSheetRows = recommendation.map(transaction => {
return [
transaction.from,
transaction.to,
transaction.amount
];
});
const dataToWrite = [recommendationSheetHeaders, ...recommendationSheetRows];
recommendationSheet.getRange(1, 1, dataToWrite.length, recommendationSheetHeaders.length).setValues(dataToWrite);
SpreadsheetApp.flush();
}