Upgrade to Pro — share decks privately, control downloads, hide ads and more …

GASで在庫管理をサクッと実装した話

tekihei2317
October 08, 2021
450

 GASで在庫管理をサクッと実装した話

tekihei2317

October 08, 2021
Tweet

Transcript

  1. 目次 1. ある日のこと 2. Google App Script (GAS )とはなにか 3.

    先入先出法について 4. 作ったプログラム 5. clasp でローカルでGAS を書く 6. まとめ by @tekihei2317
  2. ある日のこと 上長「在庫管理したいんだけど、スプレッドシートでこれ出来る?」 上長「先入先出法っていうのがあって... 」 ぼく「なるほど... 」 ~~ 30 分後 ~~

    ぼく「スプレッドシートだと難しいと思います!」 ぼく「プログラムを書きたくなるやつですね」 by @tekihei2317
  3. 先入先出法とは 先に仕入れた商品から先に販売したと仮定して、棚卸資産(在庫の価 値)を計算する方法。 例) りんごを300 円×30 個、500 円×20 個、400 円×40

    個の順に仕入れる 棚卸資産は、 20 個販売したとき 300×10+500×20+400×40 = 29000 円 40 個販売したとき 500×10+400×40 = 21000 円 by @tekihei2317
  4. Google App Script ( GAS )とは Google のサービスと連携したアプリケーションを作るためのプラッ トフォーム Gmail

    、Google Drive 、Google Calender 、etc... JavaScript で書ける! 処理系がChrome と同じV8 エンジン by @tekihei2317
  5. 作ったプログラム(入力) 在庫の個数と価値のペアをシートから読み込む const makeStocks = (lastRowIndex) => { const stocks

    = []; for(let i = OFFSET_ROW; i <= lastRowIndex; i++){ const count = sheet.getRange(i, OFFSET_COL).getValue(); if (count > 0) { const price = sheet.getRange(i, OFFSET_COL + 1).getValue(); stocks.push({ count, price }); } } return stocks; }; by @tekihei2317
  6. 作ったプログラム(計算) 在庫と引き出した個数の累計から、引き出した在庫の価値を求める > const stocks = [ ... { price:

    300, count: 3 }, ... { price: 500, count: 2 }, ... { price: 400, count: 3 }, ... ]; > const consumedCounts = [0, 0, 0, 2, 4, 5, 8]; > let calcConsumedPriceSum; > import("./src/lib/calcConsumedPriceSum.js").then((module) => calcConsumedPriceSum = module.default); > calcConsumedPriceSum(stocks, consumedCounts); [ 0, 0, 0, 600, 1400, 1900, 3100 ] by @tekihei2317
  7. 作ったプログラム(出力) 計算した値をセルに出力する const setPriceSum = (priceSumList) => { priceSumList.forEach((priceSum, index)

    => { sheet.getRange(OFFSET_ROW + 1 + index, CONSUMED_COUNT_COL + 1).setValue(priceSum); }); } by @tekihei2317
  8. 作ったプログラム(トリガーの設定) セルを変更したときに計算を実行する const onEdit = () => { const lastRowIndex

    = calcLastRowIndex(); const stocks = makeStocks(lastRowIndex); const priceSumList = calcResult(stocks, lastRowIndex); setPriceSum(priceSumList); clearPriceSum(priceSumList.length); }; by @tekihei2317