Scrapboxで四則演算をするUserScript
#Scrapbox
https://gyazo.com/418ab366b6d8f1d42564dda9924359aa
概要
1000+2000の様な数式を選択すると計算結果がポップアップされる.
計算結果をクリックすると与式=答が挿入される.
できること
10,000+20,000の様にカンマ区切りになっている場合でも計算できる.
計算結果が割り切れない場合は,小数第五位を四捨五入して小数第四位までで表示.
roundThresholdで制御
できないこと
四則演算以外の計算はできない
Math.conとか使いたい場合は,ScrapCalc - 増井俊之 が参考になりそう.
改行を含む数式は計算できない.
複数行の総和が取りたいときはScrapboxで行の総和を計算するUserScriptで計算できる
計算例
(1+2)*(2+2)=12
1/3=0.3333
10,000+20,000=30000
Script
code:script.js
const calcText = text => {
if (!text||text.indexOf("\n")!=-1) {
return;
}
const f1 = text.match(/(0-9.(),++-/*+0-9.(),+)/);
if (!f1 || f10 == '') {
return;
}
const found = text.match(/(0-9.(),+-/*)+/);
if (!found || found0 == '') {
return;
}
try {
const roundThreshold=10000;
const formula = found0.replaceAll(",","");
return found0 + "=" + Math.round(new Function("return "+formula)()*roundThreshold)/roundThreshold;
} catch (error) {
// console.error(error);
return;
}
};
scrapbox.PopupMenu.addButton({
title: text => calcText(text),
onClick: text => calcText(text)
});