function updateIncomeTyler() {
const baseUrl = "appfolio.com"
var url = "appfolio.com/api/v1/reports/income_statement.json?property_groups=186";
const auth = "Basic " + Utilities.base64Encode("*ClientID:ClientSecret*"); // Credentials hidden
let options = {
method: "get",
headers: {
Authorization: auth
},
muteHttpExceptions: true
};
let response = UrlFetchApp.fetch(url, options);
let json = JSON.parse(response.getContentText());
let results = json;
var ss = SpreadsheetApp.getActive();
var srcSheet = ss.getActiveSheet();
srcSheet.getRange("A:K").clearContent();
while (json["next_page_url"]) {
let nextUrl = baseUrl + json["next_page_url"];
response = UrlFetchApp.fetch(nextUrl, options);
json = JSON.parse(response.getContentText());
Logger.log(json);
results = results.concat(json)
}
writeFullJsonToSheet(results);
}
function writeFullJsonToSheet(jsonData) {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1")
sheet.clearContents();
// If it's an array of objects
if (Array.isArray(jsonData)) {
if (jsonData.length === 0) {
sheet.getRange(1, 1).setValue("No data in array.");
return;
}
const headers = Object.keys(jsonData[0]);
const data = jsonData.map(obj => headers.map(header => obj[header]));
data.unshift(headers); // Add headers at top
sheet.getRange(1, 1, data.length, headers.length).setValues(data);
} else if (typeof jsonData === 'object') {
// If it's a plain object, convert key-value pairs into rows
const entries = Object.entries(jsonData);
const data = entries.map(([key, value]) => [key, typeof value === 'object' ? JSON.stringify(value) : value]);
sheet.getRange(1, 1, data.length, 2).setValues(data);
} else {
// Just write it as a string
sheet.getRange(1, 1).setValue(String(jsonData));
}
}