LoginSignup
1
2

More than 1 year has passed since last update.

【Workato】便利なformulaの使い方やデータ操作一覧【随時更新】

Last updated at Posted at 2021-03-06

Workatoにおけるデータ操作

Workatoではformulaモードを使うことでExcelの関数のような感覚でデータの前処理や変換が可能である。
もし、さらに複雑なデータ変換や処理をしたい場合、「Ruby」コネクターや「JavaScript」コネクターが用意されているので、レシピの中にコードを埋め込むことも可能だ。(滅多に使うことはない。が、オプションとして存在するがために痒い所に手が届く。)
この記事では便利なformulaやその活用方法、Rubyのサンプルコードなどをまとめている。(随時更新)

リストから特定の要素だけ抜き出したい

[ARRAY].where("condition": "value")

例えばSensesSmartHRのAPIはカスタムフィールドはリストに入って返ってくる。その中から特定のカスタムフィールドだけ参照したことがあるだろう。
そういうときはこういったformulaを使う。

[案件詳細項目].where(name: "プロジェクト終了予定日")[0]['value']

スクリーンショット 2021-03-06 16.00.30(2).png

ちなみに、階層構造のオブジェクトで.where()を使いたい場合

.where("parentkey.childkey": "condition")

入れ子になったリストから特定の要素だけを取り出す

[ARRAY].pluck(column_name)

Workdayから取れてくるデータのように、複雑に入れ子になっているデータの処理には.pluck()が便利。

[
  {
    "name": "Jake"
  },
  {
    "name": "Kate"
  }
].pluck("name")

#["Jake", "Kate"]
[
  {
    "Custom_ID_Data": {
      "ID": "0000001",
      "ID_Type_Reference": "00EAUEDI23983"
    }
  },
  {
    "Custom_ID_Data": {
      "ID": "0000002",
      "ID_Type_Reference": "00VIEIO495310"
    }
  }
].pluck(["Custom_ID_Data", "ID"], ["Custom_ID_Data", "ID_Type_Reference"])

#[["0000001","00EAUEDI23983"],["0000002","00VIEIO495310"]]

String型のデータをリスト型のデータに変換

例えば,区切りの文字列をバラしてリストにしたい、といったときに使えるrubyアクション。

string_to_list.rb
string = input['string_to_transform'] #string
output_list = [] #array

string.split(",").each do |item| #push each item in string to array
  output_list << {alphabets: item}
end

{output_list: output_list} #return array

サンプルのコミュニティレシピ

ランダムな文字列(パスワード等)を生成

generate_random_password.rb
password = (('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a + ['~','!','@','#','$','%','^','&','*','(',')','-','+']).sort_by{rand}.first(8).join

{password: password}

サンプルのコミュニティレシピ

Shift_JISにエンコーディングするときに、対応できない文字を「?」に置き換える

[DATA_PILL].encode("Shift_JIS", { :invalid => :replace, :undef => :replace, :replace => "?" })

現在時刻を任意のフォーマットで取得

now.in_time_zone("Asia/Tokyo").strftime("%Y/%m/%d %H:%M")

全角文字、半角文字の抽出

"ゼンカクハンカク".scan(/[^ -~。-゚]/).join
"ゼンカクハンカク".scan(/[ -~。-゚]/).join

数値に3桁区切りでカンマを挿入

10000.to_s(:delimited) #10,000

nullの可能性があるデータピルに対してformulaを使用する

10000.to_s(:delimited) #10,000

formula内でLookup Tableを参照

lookup('JIS X 0401都道府県コード', 都府県名: "群馬県")["コード"]

#10

下図のようにLookup entryアクションを使用してエントリをLookupするのと同じ動作。(この際取得できるエントリの「コード」列を参照)
image.png

1
2
0

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
1
2