-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppScript.js
More file actions
112 lines (96 loc) · 3.81 KB
/
AppScript.js
File metadata and controls
112 lines (96 loc) · 3.81 KB
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
/**
* Custom function to fetch the average price for an item by its TypeID using GESI's markets_prices function.
* @param {number} typeID The TypeID of the item you want to look up.
* @return {string} The average price of the item, or an error message if not found.
* @customfunction
*/
function getAveragePrice(typeID) {
try {
// Fetch market prices using the markets_prices function for the given TypeID
var result = GESI.invoke("markets_prices", { type_id: typeID });
// Check if result data is available
if (result && result.length > 0) {
// Iterate through the result to find the matching TypeID and get the average price
for (var i = 0; i < result.length; i++) {
// The TypeID is in the third column (index 2)
if (result[i][2] == typeID) {
var averagePrice = result[i][1]; // The average price is in the second column (index 1)
return averagePrice ? averagePrice : "Price not found";
}
}
return "TypeID not found in market data";
} else {
return "No market data available";
}
} catch (e) {
return "Error: " + e.message;
}
}
/**
* Returns the minSell and MaxBuy price for the provided typeId at the provided locationID
*
* @param {number} typeId to get privve data for
* @param {number} locationID to filter on. Can either be a structre or a station ID.
* @param {number} regionID to filter on. Only required if locationId is a station ID.
*
* @customfunction
*/
/**
* Fetches minimum sell price and maximum buy price for a given item and location.
* @param {number} typeID - The TypeID of the item.
* @param {number} locationID - The location ID to filter orders (structure or station).
* @param {number} regionID - The region ID to fetch orders from if location is not a structure.
* @return {Array} [minSell, MaxBuy] - Minimum sell price and maximum buy price.
* https://www.youtube.com/watch?v=sRiLmEhsWW0
*/
function typePriceData(typeID, locationID, regionID) {
var data = locationID > 10000000000000
? GESI.invokeRaw("markets_structure_structure", { structure_id: locationID })
: GESI.invokeRaw("markets_region_orders", { region_id: regionID, order_type: "all", type_id: typeID });
var minSell = null;
var maxBuy = null;
// Process each order
data.forEach(function(order) {
if (order.type_id === typeID) {
if (order.is_buy_order) { /
if (!maxBuy || order.price > maxBuy) {
maxBuy = order.price;
}
} else { // Sell order
if (!minSell || order.price < minSell) {
minSell = order.price;
}
}
}
});
// Return results
return [[maxBuy, minSell]];
}
//menu function
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('Time Stamp Menu')
.addItem('Save Data','saveDataToSameSheet')
.addToUi();
}
function saveDataToSameSheet() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheets()[0];
try {
//range A4:Q51
const snapshotRange = sheet.getRange('A4:P51');
const snapshotData = snapshotRange.getValues();
const lastRow = sheet.getLastRow();
// Add timestamp
const timestamp = new Date().toLocaleString();
sheet.getRange(lastRow + 2, 1).setValue('Snapshot: ' + timestamp);
// Copy headers for new set of stuff
const headers = sheet.getRange('A3:P3').getValues()[0];
sheet.getRange(lastRow + 3, 1, 1, headers.length).setValues([headers]);
// Paste all the data or it doesn't work because you forgot an index. And then forget a comma
sheet.getRange(lastRow + 4, 1, snapshotData.length, snapshotData[0].length).setValues(snapshotData);
SpreadsheetApp.getUi().alert('Snapshot saved');
} catch (error) {
SpreadsheetApp.getUi().alert('Error taking snapshot: ' + error.toString());
}
}