LoginSignup
5
2

More than 3 years have passed since last update.

[JANコード] GoogleSpreadsheetでチェックデジットを求める

Last updated at Posted at 2018-06-27

これはなに?

GoogleSpreadSheetで使える独自関数です。
商品を流通させる時に便利なJANコードで使用するチェックデジットを求めます。

様々なサイトで求めることが出来ますが、SpreadSheet内で使えるものが、ちょっと検索して見つからなかったので作りました。大したものではないですが、よければ使ってください。

計算式は、このページを参考にしました。
http://www.dsri.jp/jan/check_digit.html

独自関数の作り方はこちらを参照してください。
https://qiita.com/soundTricker/items/c9fa4470c59ef677dd5a

 ソース

function CHECK_DIGIT(office_num, product_num) {

  var check_string = office_num + product_num + "";

  // 標準タイプ(13文字)のみ対応 チェックデジットを末尾に足して13文字になる
  if(check_string.length != 12) return "LENGTH_NOT_12";

  var total_even = 0;
  for(var idx = 1; idx < check_string.length; idx += 2) total_even += parseInt(check_string[idx]);
  var total_odd = 0;
  for(var idx = 0; idx < check_string.length; idx += 2) total_odd  += parseInt(check_string[idx]);

  var three_times_total_even = total_even * 3;
  var total = three_times_total_even + total_odd;
  var last_num = total % 10;

  if(last_num == 0){
    return 0; 
  }else{
    return 10 - last_num;
  }
}

出力例

事業者番号が 123456789 で、
商品番号が 001 の場合、
チェックデジットは 2 になります。

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