LoginSignup
2
2

More than 3 years have passed since last update.

売掛金の増減分析を自動でやってみる。

Last updated at Posted at 2019-05-26

この記事の目的

  • 会計監査資料の作成が面倒だったため自動化を行うこと
  • 資料作成コストを削減するための手法としての利用
  • @Kirin8210 さんとQiita記事を書く流れになったため

前提

  • freeeAPIを叩くことで試算表を取得している
  • 情報のアウトプットを2次元配列にしており、スプレッドシートに出力する仕組み
  • OAuth2ライブラリを活用して、AccessTokenを取得している
code.ts
/**
 * APIを叩いて二期分のBS得意先明細を取得する
 * @return {Array} resBss
 */
function getTwoYearsBsCustomer() {
  const accessToken = getService().getAccessToken();
  const baseUrl = 'https://api.freee.co.jp/api/1/reports/trial_bs_two_years';
  const postUrl =
    baseUrl +
    setting().companyId +
    setting().fiscalYear +
    setting().startMonth +
    setting().endMonth +
    setting().partner;
  const options = {
    method: 'get',
    headers: {
      Authorization: 'Bearer ' + accessToken
    }
  };
  const resBss = JSON.parse(UrlFetchApp.fetch(postUrl, options))
    .trial_bs_two_years.balances;
  return resBss;
}

/**
 * 勘定科目明細に必要なArrayを返す
 * @param {string}account
 */
function getChangeAnalysis(account: string) {
  const Bss = getTwoYearsBsCustomer();
  const a = Bss.filter(item => item.account_item_name == account);
  //[得意先名, 前期末, 当期末, 増減額, 増減率]
  let array = a[0].partners.map(function(customer) {
    const last = customer.last_year_closing_balance;
    const end = customer.closing_balance;
    return [customer.name, last, end, end - last, end / last];
  });
  array.unshift(['得意先名', '前期末', '当期末', '増減額', '増減率']);
  return array;
}

/**
 * 関数を実行する場合はこちらを実行ください。
 * @param {string}account
 */
function main(){
  const array = getChangeAnalysis(売掛金);
  setSpreadSheet(array)//任意のスプレッドシートにarrayをセットする。
}


工夫

  • 出力形式の操作を行うことにより、品目別・部門別等の増減分析を自動化できる
  • アウトプットでのスプレッドシート加工方法によっては、月次推移だったり諸々使える
  • getChangeAnalysis(account: string)については、引数を変えるだけアウトプットが変わること

追記

これらのQiitaに書ききれない内容は同人誌にまとめようとおもうので、お楽しみに。

2
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2