Open gcor71 opened 10 months ago
After a lot of trial and error, and some help from ChatGPT, I got it to work as follows:
in OSM.gs
/////////////////
//
// Section size statistics
//
/////////////////
fetch_stats() {
var result = this.fetch("ext/discounts/?action=getSizes&code=221§ionid=68972");
return result;
}
in testcode.gs
function action_fetch_stats() {
var service = getOSMService();
var osm = OSM(service).init();
var jsonData = osm.fetch_stats();
// Replace SHEET_NAME with the name of the sheet where you want to create the table
var sheetName = 'Section Size Stats';
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
if (!sheet) {
// If the sheet doesn't exist, create a new sheet with the specified name
sheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet(sheetName);
} else {
// If the sheet already exists, clear its contents
sheet.clear();
}
// Write headers to the sheet
var headers = [
'Group Name',
'Section Name',
'Type',
'Ideal Size',
'Ideal Leaders',
'Num Leaders',
'Num Scouts',
'Waiting'
];
sheet.appendRow(headers);
// Loop through the data and write it to the sheet
var categories = jsonData.categories;
for (var i = 0; i < categories.length; i++) {
var category = categories[i];
var groups = jsonData[category].byGroup;
for (var groupName in groups) {
var group = groups[groupName];
for (var j = 0; j < group.sections.length; j++) {
var section = group.sections[j];
var rowData = [
groupName,
section.name,
section.type,
section.idealsize,
section.idealleaders,
section.numleaders,
section.numscouts,
section.waiting
];
sheet.appendRow(rowData);
}
}
}
}
Hey, I'm looking at how to adapt your project to pull Section Size information into a sheet. It's not part of OSM many folks will see/use.
Would you take a look at the output below and suggest which of your existing .gs components would be closest to what I'd need to do to pull the "sections" elements per group? I am happy to work on it myself and share back, but I struggle to interpret the function calls necessary to navigate across the array. It's probably pretty simple - but I can't get my head round it.
Thanks in advance.
The POST is: /ext/discounts/?action=getSizes&code=221§ionid=68972 The returned BODY (sample) is: