Skip to content

Instantly share code, notes, and snippets.

@audreyt
Created September 30, 2014 10:12
Show Gist options
  • Save audreyt/8b88ef548cb8b82faff5 to your computer and use it in GitHub Desktop.
Save audreyt/8b88ef548cb8b82faff5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env lsc
XLSX = require \xlsx
sheet_to_socialcalc = do ->
const header = """
socialcalc:version:1.5
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=SocialCalcSpreadsheetControlSave
"""
const sep = """
--SocialCalcSpreadsheetControlSave
Content-type: text/plain; charset=UTF-8
"""
# TODO: the other parts
const meta = """
# SocialCalc Spreadsheet Control Save
part:sheet
"""
const end = "--SocialCalcSpreadsheetControlSave--"
scencode = -> it.replace(/\\/g, "\\b").replace(/:/g, "\\c").replace(/\n/g,"\\n")
scsave = (ws) ->
return unless ws?['!ref']
o = []
oo = []
r = XLSX.utils.decode_range ws['!ref']
for R from r.s.r to r.e.r
for C from r.s.c to r.e.c
coord = XLSX.utils.encode_cell {r:R,c:C}
cell = ws[coord]
console.log cell
continue unless cell?v?
oo = ["cell", coord, 't']
switch cell.t
| <[ s str ]> => oo.push scencode(cell.v)
| 'n' =>
if cell.f
oo[2] = 'vtf'
oo.push 'n'
oo.push cell.v
oo.push scencode(cell.f)
else
oo[2] = 'v'
oo.push cell.v
o.push oo.join(":")
o.push "sheet:c:#{ (r.e.c - r.s.c + 1) }:r:#{ (r.e.r - r.s.r + 1) }:tvf:1"
o.push "valueformat:1:text-wiki"
o.push "copiedfrom:#{ ws['!ref'] }"
return o.join "\n"
(ws, opts) -> [header, sep, meta, sep, scsave(ws), end].join("\n")
process.argv.shift! if process.argv.0 is /(?:lsc|node(?:js)?)$/
if process.argv.length > 1
wb = XLSX.readFile process.argv.1
console.log sheet_to_socialcalc wb.Sheets[wb.SheetNames[0]]
else
console.log "Please specify a .xlsx file as argument."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment