Closed11

Notionのデイリーページを自動作成

eetann / えーたんeetann / えーたん

概要

Notionのあるデータベース内に、今日の日付をタイトルに含むページが存在しなければ作成する。最後に、そのページのURLを返す。

NotionのAPIとGoogle Apps Scriptは久しぶりに使うので思い出すところから。更新による差分めっちゃありそう。

eetann / えーたんeetann / えーたん
  • claspの再導入
  • プロジェクトの作成
  • Hello, worldを返すやつ作る
  • NotionのAPIでデータベースと連携する
    • データベースの情報だけ返すやつ作る
  • データベースから今日の日付をタイトルに含むページが存在するか返す
  • データベースに今日のデイリーページが存在しなければそのページを作成してURLを返す
  • デイリーページの作成中に日付も設定する
  • デイリーページの作成中にアイコンも設定する
  • 毎日0:01に叩くようにする

以下はできないのでやらない

  • 任意の端末から叩いてURLを取得できることを確認
    • PCのブラウザ
    • iPadのショートカット
eetann / えーたんeetann / えーたん

clasp再導入とプロジェクトの作成

以下に従ってGoogle Apps Script APIをオンにした。
https://github.com/google/clasp#install

リポジトリを作成してブランチ切った。
https://github.com/eetann/notion-daily-generator

パッケージの準備

npm init --yes
npm install typescript --save-dev
mkdir src
tsconfig.json
{
  "compilerOptions": {
    "lib": ["esnext"],
    "experimentalDecorators": true,
    "target": "ES2019",
    "outDir": "./dist"
  },
  "include": ["src/**/*"]
}

参考:

npm install clasp @types/google-apps-script
npx clasp login

~/.clasprc.jsonが作成される。

npm install --save-dev\
    eslint \
    prettier \
    eslint-config-prettier \
    eslint-import-resolver-typescript \
    eslint-plugin-{import,prettier,unused-imports} \
    @trivago/prettier-plugin-sort-imports \
    @typescript-eslint/parser \
    @typescript-eslint/eslint-plugin

いつもの。

.eslintrc.js
module.exports = {
  env: {
    browser: true,
    es2021: true,
  },
  extends: [
    "eslint:recommended",
    "plugin:@typescript-eslint/recommended",
    "prettier",
  ],
  parser: "@typescript-eslint/parser",
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: "latest",
    sourceType: "module",
  },
  plugins: ["@typescript-eslint", "unused-imports"],
  rules: {
    "@typescript-eslint/no-unused-vars": "off",
    "unused-imports/no-unused-imports": "error",
    "unused-imports/no-unused-vars": [
      "warn",
      {
        vars: "all",
        varsIgnorePattern: "^_",
        args: "after-used",
        argsIgnorePattern: "^_",
      },
    ],
  },
  overrides: [],
};

以下もあとで追加するかも?

npm install --save-dev eslint-plugin-googleappsscript
eetann / えーたんeetann / えーたん

実際にスクリプトを作成

npx clasp create --type webapp --title notion-daily-generator

src/.clasp.jsonrootDir./distに変更する。
また、src/.clasp.jsonはGitHubで管理したくない&GitHub Actionsでsecretsとして使えばいいので.gitignoreに加えた。

生成されたappsscript.jsontimeZoneAsia/Tokyoに変更した。

package.jsonscriptsに以下を加えた(まだ使ってない=未検証)。

package.json
    "lint": "eslint src && prettier --check src",
    "deploy": "cp appsscript.json dist/appsscript.json && clasp push",

参考:Google Apps Script開発環境をVisual Studio Codeで構築する | たぬきうどんTechBlog

eetann / えーたんeetann / えーたん

clasp--rootDirは文字通りプロジェクトとしてのルートディレクトリを指定するので、デフォルトのまま(カレントディレクトリ)でOK。
--rootDir ./srcとかしたらファイルの移動とか面倒。

eetann / えーたんeetann / えーたん
このスクラップは2022/08/19にクローズされました