2024年2月11日 (日曜日)

09:07:58 # Life Google DocsのGASで自動でエントリーを追加する。 日記を書いていたら毎回毎日の日付を入力するの面倒だなぁ、 毎週の会議のその週のエントリーを追加したりするのが面倒だとおもいませんか?僕は思うので、自動化しました。 onOpenメソッドを用意しておくと開いたときに起動するスクリプトが用意できて、その中で日付を確認して追加することができます。 例えば日記ドキュメントを用意しておくと最初の行に最新の日付がついていなかったら追加するというのはこれでできます。あら便利。

/**
 * @OnlyCurrentDoc
 */

function onOpen(e) {
  DocumentApp.getUi().createAddonMenu()
      .addItem('Add today', 'addToday')
      .addToUi();

  addToday();
}

/**
 * Add today's date string to top of the document.
 */
function addToday() {
  if (!LockService.getDocumentLock().tryLock(10000)) {
    Logger.log("Could not get lock");
    return;
  }

  var body = DocumentApp.getActiveDocument().getBody();
  var paragraphs = body.getParagraphs();
  var firstParagraph = paragraphs[0].getText();
  var d = new Date();
  var todayDate = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate();
  Logger.log('today is %s and your doc says %s', todayDate, firstParagraph);
  if (todayDate != firstParagraph) {
    body.insertListItem(0, "").setGlyphType(DocumentApp.GlyphType.BULLET);
    body.insertParagraph(0, todayDate).setHeading(DocumentApp.ParagraphHeading.HEADING1);
    Logger.log('inserted today!');
  }
}
	

しかしLockServiceを使っているんだけど同時に複数実行するとドキュメントの変更が反映されるまえにドキュメントを参照しに来る気がする。

09:20:05 # Life Emacsで週報を書く。 ここ数年自分で週のまとめをつくっているのだが、週の最後に書くんじゃないくて毎日アップデートしている方がよいということがわかった。今週何をするかというのを毎日更新していって結果として週のまとめになっているという形式。日報形式も試したんだけど僕の場合は日報は細かすぎてなんか達成感がない。 そこで週報をかんたんに書く方法を考えるんだけど、Markdownを決まった場所においてある手元のファイルで書くというのに落ち着いた。そしてファイルを細かく分けるのも結構面倒で、巨大になるのも微妙で、結局年ごとに分けるということにした。elispでいうとこんな感じ。多分色んな人がやっておるんじゃろうなとおもうがこれがとりあえずシンプルでよかった。

(defun snippet ()
  "Open up personal weekly snippet summary file. Creates file in ~/personal-weekly/YEAR.md"
  (interactive)
  (find-file (concat "~/personal-weekly/" (format-time-string "%Y") ".md"))
  (goto-char 1)
  (if (re-search-forward "^## Weekly notes" nil t)
      t
    ;; Insert a title
    (insert "## Weekly notes\n\n"))
  (beginning-of-line)
  (next-line)
  (let ((title (concat "## " (format-time-string "Week %W "))))
    (if (re-search-forward (concat "^" title) nil t)
        ;; there was a match
        t
      (insert (concat "\n" title (format-time-string "%m-%d\n\n")
                      "-   Health\n-   Family\n-   Learning\n-   misc\n")))))
	
Junichi Uekawa