Action

Change a Dynalist Item

Posted by sorashima, Last update almost 3 years ago

UPDATES

almost 3 years ago

Added the URL of the description page.

ドラフトのデータでDynalistの1アイテムを書き換える。

https://sorashima.hatenablog.com/entry/EditDynalistItemOnDrafts

(sorry. in Japanese.)

Steps

  • script

    // CredentialにDynalistのSecret Tokenを保存
    const credential = Credential.create("Dynalist", "Dynalist")
    credential.addPasswordField("token", "Secret Token")
    credential.authorize()
    
    
    // draftのYAML部のチェック
    let emA = []
    let title
    let file_id
    let node_id
    let checked
    let checkbox
    let heading
    let color
    
    
    // 最低でもtitleとYAML部を合わせた9行必要
    if (draft.lines.length < 9) {
      emA.push("タイトルとアイテムのパラメータの計9行より行数が少ないので、Dynalistのアイテムではないようです。")
    
    } else {
    
      const ty = draft.lines.slice(0, 9)
    
      // 2行目と9行目に「---」 必須
      if ( (ty[2 - 1] != "---") && (ty[9 - 1] != "---") ) {
        emA.push("2行目と7行目が「---」ではないので、はてなブログのエントリーではないようです。")
    
      } else {
    
        // titleは必須
        title = draft.title.trim()
        if (title == "") emA.push("タイトルが空です。")
        console.log(`title:${title}`)
    
    
        // file_idは必須
        let file_idL
        if ( (file_idL = ty.find(l => l.includes("file_id: "))) === undefined ) {
          emA.push("「file_id: 」行がありません。")
        } else {
          file_id = /file_id: (.*)$/.exec(file_idL)[1].trim()
          if ( file_id == "" ) emA.push("file_idが読み込まれていません。")
        }
        console.log(`file_id:${file_id}`)
    
    
        // node_idは必須
        let node_idL
        if ( (node_idL = ty.find(l => l.includes("node_id: "))) === undefined ) {
          emA.push("「node_id: 」行がありません。")
        } else {
          node_id = /node_id: (.*)$/.exec(node_idL)[1].trim()
          if ( node_id == "" ) emA.push("node_idが読み込まれていません。")
        }
        console.log(`node_id:${node_id}`)
    
    
        // checked行は必須。値は、空あるいはfalse、またはtrue。
        let checkedL
        if ( (checkedL = ty.find(l => l.includes("checked: "))) === undefined ) {
          emA.push("「checked: 」行がありません。")
        } else {
          checked = /checked: (.*)$/.exec(checkedL)[1].trim()
          if ( !["true", "false", ""].includes(checked) ) {
            emA.push("checkedは、空あるいは「false」、または「true」を指定。")
          } else {
            checked = (checked == "true") ? true : false
          }
        }
        console.log(`checked:${checked}`)
    
    
        // checkbox行は必須。値は、空あるいはfalse、またはtrue。
        let checkboxL
        if ( (checkboxL = ty.find(l => l.includes("checkbox: "))) === undefined ) {
          emA.push("「checkbox: 」行がありません。")
        } else {
          checkbox = /checkbox: (.*)$/.exec(checkboxL)[1].trim()
          if ( !["true", "false", ""].includes(checkbox) ) {
            emA.push("checkboxは、空あるいは「false」、または「true」を指定。")
          } else {
            checkbox = (checkbox == "true") ? true : false
          }
        }
        console.log(`checkbox:${checkbox}`)
    
    
        // heading行は必須。値は、空あるいは0、または3までの数字
        let headingL
        if ( (headingL = ty.find(l => l.includes("heading: "))) === undefined ) {
          emA.push("「heading: 」行がありません。")
        } else {
          heading = /heading: (.*)$/.exec(headingL)[1].trim()
          if ( !["", "0", "1", "2", "3"].includes(heading) ) {
            emA.push("headingは、空あるいは0、または3までの数字指定。")
          } else {
            heading = (heading == "") ? 0 : parseInt(heading)
          }
        }
        console.log(`checked:${checked}`)
    
    
        // color行は必須。値は、空あるいは0、または6までの数字
        let colorL
        if ( (colorL = ty.find(l => l.includes("color: "))) === undefined ) {
          emA.push("「color: 」行がありません。")
        } else {
          color = /color: (.*)$/.exec(colorL)[1].trim()
          if ( !["", "0", "1", "2", "3", "4", "5", "6"].includes(color) ) {
            emA.push("colorは、空あるいは0、または6までの数字を指定。")
          } else {
            color = (color == "") ? 0 : parseInt(color)
          }
        }
        console.log(`color:${color}`)
    
        
      }
    }
    
    
    // YAML部のチェックの結果、
    
    if (emA.length != 0) {
      // エラーがある場合はエラーメッセージを表示
      alert(emA.join("\n"))
      app.displayErrorMessage("適切なDyanlistのアイテムではないようです")
      context.cancel()
    
    } else {
    
      // エラーが無い場合
    
      // Dynalistのアイテムの変更
      const http = HTTP.create()
      
      const editO = {
        action: "edit",
        node_id: node_id,
        content: title,
        note: draft.lines.slice(9, draft.lines.length).join("\n"),
        checked: checked,
        checkbox: checkbox,
        heading: heading,
        color: color
      }
      const changesA = [editO]
      
      const response = http.request({
        "url": "https://dynalist.io/api/v1/doc/edit",
        "encoding": "json",
        "method": "POST",
        "data": {
          "token": credential.getValue("token"),
          "file_id": file_id,
          "changes": changesA
        }
      })
    
    
      if (response.success) {
        // HTTPのリクエストが正常に完了したら
        const rText = JSON.parse(response.responseText)
    
        if (rText._code == 'Ok') {
          // レスポンスの _code が Ok (リクエストが成功)の場合
          //app.displaySuccessMessage(rText._code)
          console.log(JSON.stringify(rText))
        } else {
          // レスポンス の _code が Ok でない場合、errorの情報を表示する
          alert(rText._code + '\n' + rText._msg)
          context.fail(rText._msg)
        }
    
      } else {
        // HTTPのリクエストが正常に完了しなかったら
        console.log(response.statusCode)
        console.log(response.error);
        alert(response.statusCode + '\n' + response.error)
        context.fail(response.error)
    
      }
      
    }

Options

  • After Success Archive
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.