Edited at

BSの二期増減分析を自動で作ってみる


目的


  • 資料自動化のため

  • 情報のこえだめとして


code.ts

/**

* @autour @kabanyasu
*/

/**
* APIを叩いて月次試算表を取得し、オブジェクト形式に変換する
* @return {object} resBss
*/

function postApi() {
let accessToken = getService().getAccessToken();
let baseUrl = 'https://api.freee.co.jp/api/1/reports/trial_bs_two_years';
let companyId = 9999999;
let fiscalYear = 2019;
let startMonth = 1;
let endMonth = 12;
let postUrl = baseUrl + `?company_id=${companyId}&fiscal_year=${fiscalYear}&start_month=${startMonth}&end_month=${endMonth}`;
let options = {
'method': 'get',
'headers': {
'Authorization': 'Bearer ' + accessToken
},
};
let resBss = JSON.parse(UrlFetchApp.fetch(postUrl, options)).trial_bs_two_years.balances;
return resBss;
}

/**
* オブジェクトを二次配列に整形する。
* @param {any} object
* @return {any} array
*/

function createArray(object: any) {
const array = object.map(account => {
const level = account.hierarchy_level;
const close = account.closing_balance;
const last = account.last_year_closing_balance;
if (!account.total_line) {
//[勘定科目ID, 勘定科目名, 階層, 前期末残高, 当期末残高, 増減率, 増減額]
return [account.account_item_id, account.account_item_name, level, last, close, close / last, close - last];
} else {
//[勘定カテゴリID, 勘定カテゴリ名, 階層, 前期末残高, 当期末残高, 増減率, 増減額]
return [account.account_category_id, account.account_category_name, level, last, close, close / last, close - last];
}
});
return array;
}