Skip to content

Instantly share code, notes, and snippets.

@ttsukagoshi
Created September 1, 2022 09:13
Show Gist options
  • Save ttsukagoshi/f193bcf3065d012369a7314cfe135333 to your computer and use it in GitHub Desktop.
Save ttsukagoshi/f193bcf3065d012369a7314cfe135333 to your computer and use it in GitHub Desktop.
5年後の年度末を自動的に計算して置換するための、Google Docs-boundのGoogle Apps Script。スクリプトを実行すると、特定の文字列(ここでは `<<年>>` )を、所定の年度末の年に置き換える。
const YEARS_AFTER = 5; // 5年後
function onOpen() {
DocumentApp.getUi()
.createMenu('期限入力')
.addItem('年挿入', 'enterYear')
.addToUi();
}
function enterYear() {
const rePattern = '<<年>>';
const now = new Date();
const nowJstYear = parseInt(Utilities.formatDate(now, 'JST', 'yyyy')); // 日本時間での西暦4桁
const nowJstMonth = parseInt(Utilities.formatDate(now, 'JST', 'M')); // 日本時間での月
let year = nowJstYear + YEARS_AFTER;
if (nowJstMonth > 3) {
// 4〜12月であれば翌年の3月末なので
year += 1;
}
DocumentApp.getActiveDocument()
.getBody()
.editAsText()
.replaceText(rePattern, year);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment