[InDesign] [Illustrator] [ExtendScript] 選択したISBNコード/JANコード/雑誌コードのチェックディジットが正しいかどうかをチェックします
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!function() { | |
if (app.documents.length === 0) { | |
return; | |
} | |
if (app.name === "Adobe InDesign" && app.activeDocument.selection.length === 1) { | |
var result = checkDigit(app.activeDocument.selection[0]); | |
if (result === null) { | |
return; | |
} else if (result === true) { | |
alert("正しいチェックディジットです"); | |
return; | |
} else { | |
alert("正しいチェックディジットは" + result + "です"); | |
return; | |
} | |
} | |
if (app.name === "Adobe Illustrator" && app.activeDocument.selection.length === 13) { | |
var result = checkDigit(selection); | |
if (result === null) { | |
return; | |
} else if (result === true) { | |
alert("正しいチェックディジットです"); | |
return; | |
} else { | |
alert("正しいチェックディジットは" + result + "です"); | |
return; | |
} | |
} | |
}(); | |
function checkDigit (tgt) { | |
if (!tgt.hasOwnProperty("contents")) { | |
alert("テキストが選択されていません"); | |
return null; | |
} | |
tgt = tgt.contents.replace(/\-/g, ""); | |
if (!/[0-9]+/.test(tgt)) { | |
alert("テキストに半角数字以外が含まれています"); | |
return null; | |
} | |
if (tgt.length !== 13) { | |
alert("数字が13桁ではありません"); | |
return null; | |
} | |
tgt = tgt.split(""); | |
var lastDigit = tgt[tgt.length - 1]; | |
var res = [0, 0]; | |
for (var i=0; i<tgt.length-1; i++) { | |
if (i === 0 || (i % 2) === 0) { | |
res[1] += tgt[i] * 1; | |
} else { | |
res[0] += tgt[i] * 3; | |
} | |
} | |
res = res[0] + res[1]; | |
if (res % 10) { | |
res = 10 - (res % 10); | |
} else { | |
res = 0; | |
} | |
if (res == lastDigit) { | |
return true; | |
} else { | |
return res; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment