How to convert Google spreadsheet's worksheet string id to integer index (GID)?

前端 未结 6 1899
遇见更好的自我
遇见更好的自我 2020-12-02 17:46

To export google spreadsheet\'s single worksheet to CSV, integer worksheet index(GID) is required to be passed.

https://spreadsheets.google.com/feeds

6条回答
  •  天命终不由人
    2020-12-02 18:24

    This is a Clojure adaptation of Buho's and Julie's code which should work with both the new Google Sheets and with the legacy Google Spreadsheets.

    (defn wid->gid [wid]
      (let [new-wid? (> (.length wid) 3)
            wid      (if new-wid? (.substring wid 1) wid)
            xor-val  (if new-wid? 474 31578)]
        (bit-xor (Integer/parseInt wid 36) xor-val)))
    
    (defn gid->wid [gid]
      (let [new-gid? (> gid 31578)
            xor-val  (if new-gid? 474 31578)
            letter   (if new-gid? "o" "")]
        (str letter (Integer/toString (bit-xor gid xor-val) 36))))
    

提交回复
热议问题