title: office section: office
The ArozOS Office suite's documents (src/web/Office/) and converters
between its webapps and common office file formats. Backed by mod/office
(pure Go, no external dependencies), one package per format: docx, xlsx
and pptx (OOXML import and export), odf (OpenDocument), pdf, and the
import-only doc, xls and ppt for the Office 97-2003 binary formats.
Docs, Sheets and Slides keep their documents in .docx / .xlsx / .pptx:
the OOXML every office application reads, with the editor's own envelope
({type:"arozos/office", app, version, meta, body}) embedded in the package
so the suite reopens exactly what it saved. saveDocument / loadDocument
are that open and save; the other functions convert a bare body.
office.saveDocument(envelopeJson, destVpath)Write an editor envelope as the Office file destVpath's extension names
(.docx for a document, .xlsx for a spreadsheet, .pptx for a
presentation - any other pairing throws). The body is rendered to OOXML
and the envelope is embedded in the package with a fingerprint of every
OOXML part. media?file= links in the body (pictures, video, audio) are
read server side with the calling user's read permission, once each, and a
picture that is also an OOXML part is stored once. Returns true.
requirelib("office");
if (office.saveDocument(content, "user:/Documents/report.docx")){
sendResp("OK");
}
office.loadDocument(srcVpath, workdirBase)Open a .docx / .xlsx / .pptx as an editor envelope JSON string.
When the file carries a current embedded envelope - the suite wrote it and
nothing has changed it since - that is returned as saved; otherwise (a file
from Word, Excel or PowerPoint, or one they have re-saved) the OOXML is
imported and wrapped in a fresh envelope. The Office 97-2003 files of the
same kind (.doc / .dot, .xls / .xlt, .ppt / .pot / .pps) open
here too: they are converted to OOXML first and then imported the same way.
A legacy file is recognised by its content, so one that belongs to another
app (a workbook named .doc) throws instead of opening as the wrong kind.
Either way the document's media
is written into <workdirBase>/<doc-hash>/ and referenced by media?file=
links, so the JSON stays small (the Office webapps use one folder per editor
window, tmp:/.appdata/Office/cache/<window>, deleted when the window closes -
see office.releaseWorkdir). A picture already in that folder is not written
again.
office.saveDocument refuses to save (a MediaExpired error) when a picture
the document links under the Office working folders can no longer be read -
the nightly tmp sweep removed it - rather than write the file without it.
requirelib("office");
var envelope = office.loadDocument("user:/Documents/deck.pptx", "tmp:/.appdata/Office/cache/k3x9a2f0");
sendJSONResp('{"envelope":' + envelope + '}');
office.readPayload(vpath)Return the text of a request payload the front end uploaded as a file
instead of posting it (OfficeApp.agirunLarge), gunzipped when it is gzip
(capped at 512 MB once inflated). The caller deletes the file afterwards.
var body = office.readPayload(dataFile);
filelib.deleteFile(dataFile);
office.touchWorkdir(vpath, ...)Refresh the modification time of each folder, everything in it and every
folder above it up to the root of its storage, so the nightly tmp sweep (which
removes whatever has not changed for a day, -tmp_time) leaves an open editor
window's working copies alone. Only folders inside the Office working folders
are accepted (tmp:/.appdata/Office/cache|uploads|tmp/...); anything else
throws PermissionDenied. A folder that does not exist is skipped.
requirelib("office");
office.touchWorkdir("tmp:/.appdata/Office/cache/k3x9a2f0", "tmp:/.appdata/Office/uploads/k3x9a2f0");
office.releaseWorkdir(vpath [, olderThanSeconds])Delete an editor window's working folder with everything in it (the window
closed). With an age, delete instead only the entries of the folder whose
newest file is older than that - a sweep of leftovers, which may also be
given one of the working roots themselves (tmp:/.appdata/Office/cache, or
the user:/.appdata/Office/cache|uploads|tmp folders earlier versions used).
Same folder restriction as touchWorkdir.
requirelib("office");
office.releaseWorkdir("tmp:/.appdata/Office/cache/k3x9a2f0"); // the window closed
office.releaseWorkdir("tmp:/.appdata/Office/cache", 24 * 60 * 60); // leftovers a day old
office.pptxToPresentation(srcVpath)Parse a PowerPoint .pptx file into the Slides document body schema (see
src/web/Office/common/CONTRACT.md). Returns the body as a JSON string,
or throws on failure. Embedded pictures are inlined as data: URLs; text
boxes, preset shapes, connector lines and tables map to their Slides object
types. Unsupported content (video, native charts, SmartArt) is skipped.
requirelib("office");
var bodyJson = office.pptxToPresentation("user:/Desktop/deck.pptx");
sendJSONResp('{"body":' + bodyJson + '}');
office.presentationToPptx(bodyJson, destVpath)Build a .pptx from a serialized Slides body JSON string and write it to
destVpath. Pictures may be data: URLs or media?file= links (read with
the caller's permission); chart objects should carry a PNG in props.png.
Slide transitions and click links (props.link) are written as PowerPoint's
own. Video/audio objects are
not embedded: each renders as a poster picture (the captured frame in
props.png, or a generated placeholder) and the media files themselves
are packed into a sidecar zip written next to the pptx as
<dest basename>.zip. Returns true on success, or the sidecar zip's
vpath (a string, also truthy) when one was written.
requirelib("office");
var r = office.presentationToPptx(data, "user:/Desktop/out.pptx");
if (r){
// r === "user:/Desktop/out.zip" when the deck had video/audio
sendResp("OK");
}
office.xlsxToWorkbook(srcVpath)Parse an Excel .xlsx file into the Sheets document body schema. Returns
the body as a JSON string, or throws on failure. Handles values,
formulas (recalculated by the webapp), shared/inline strings, cell styles,
number formats, column widths / row heights, merged cells, frozen panes,
charts, notes, conditional formats (the rule types the editor has), tab
colours and the filter range. Pivot tables are skipped. A legacy binary
.xls is refused here; read it with office.xlsToWorkbook.
requirelib("office");
var bodyJson = office.xlsxToWorkbook("user:/Desktop/report.xlsx");
sendJSONResp('{"body":' + bodyJson + '}');
office.workbookToXlsx(bodyJson, destVpath)Build a .xlsx from a serialized Sheets body JSON string and write it to
destVpath. Returns true on success. Formulas are written natively so
Excel recalculates them; charts, notes, conditional formats, tab colours
and the filter range are written as Excel's own.
requirelib("office");
if (office.workbookToXlsx(data, "user:/Desktop/out.xlsx")){
sendResp("OK");
}
office.docxToDocument(srcVpath)Parse a Word .docx file into the Docs document body schema. Returns the
body as a JSON string, or throws on failure. Handles paragraphs,
heading/title styles, alignment, inline formatting (bold/italic/underline/
strikethrough, color, size), hyperlinks, lists, tables, embedded images
(inlined as data: URLs), header/footer text, footnotes, page geometry,
comments (body.comments) and tracked changes (as the editor's suggestion
marks). Text boxes are ignored. A legacy binary .doc is refused here;
read it with office.docToDocument.
requirelib("office");
var bodyJson = office.docxToDocument("user:/Desktop/report.docx");
sendJSONResp('{"body":' + bodyJson + '}');
office.documentToDocx(bodyJson, destVpath)Build a .docx from a serialized Docs body JSON string and write it to
destVpath. Returns true on success. Pictures may be data: URLs or
media?file= links (read with the caller's permission); an image's
data-export-src is preferred over its src. Comments and suggestion marks
become Word comments and tracked changes.
requirelib("office");
if (office.documentToDocx(data, "user:/Desktop/out.docx")){
sendResp("OK");
}
office.packToFile(envelopeJson, destVpath)Write an editor envelope as a session snapshot: a zip of document.json
plus deduplicated binary assets/. Media data URLs and media?file= links
inside the envelope become assets, so the snapshot holds the document by
itself. The Office suite writes its "Restore from previous session"
snapshots with it. Returns true on success.
office.unpackToWorkdir(srcVpath, workdirBase)Read a snapshot written by packToFile and return its envelope JSON
string with the assets extracted into <workdirBase>/<doc-hash>/ and
referenced by media?file= links instead of inline base64.
requirelib("office");
var envelope = office.unpackToWorkdir("user:/.appdata/Office/session/presentation.osession", "user:/.appdata/Office/cache");
sendJSONResp('{"envelope":' + envelope + '}');
office.odtToDocument(srcVpath)Read an OpenDocument Text file (.odt) and return the Docs body schema as
a JSON string (headings, inline formatting, links, lists, tables with
column widths and cell shading, embedded pictures as data: URLs, page
geometry, header/footer and page breaks).
office.documentToOdt(jsonStr, destVpath)Build an .odt from a serialized Docs body JSON and write it to
destVpath. Covers the same subset as the docx exporter (review markup
excepted). Pictures may be data: URLs or media?file= links. Returns
true on success.
office.odsToWorkbook(srcVpath)Read an OpenDocument Spreadsheet (.ods) and return the Sheets body
schema as a JSON string. Formulas are translated from the ODF
of:=SUM([.A1:.B2]) syntax back to plain =SUM(A1:B2) references; cell
styles, column widths, row heights, merges and cell notes
(office:annotation) are kept.
office.workbookToOds(jsonStr, destVpath)Build an .ods from a serialized Sheets body JSON and write it to
destVpath (formulas rewritten to the ODF syntax so LibreOffice
recalculates them). Returns true on success.
office.odpToPresentation(srcVpath)Read an OpenDocument Presentation (.odp) and return the Slides body
schema as a JSON string, scaled into the 960x540 editor space (text
boxes, images, basic shapes, lines, tables, slide backgrounds and speaker
notes).
office.presentationToOdp(jsonStr, destVpath)Build an .odp from a serialized Slides body JSON and write it to
destVpath. Charts export through their client-side PNG raster
(props.png), like the pptx exporter; video/audio objects are skipped.
Returns true on success.
requirelib("office");
var bodyJson = office.odsToWorkbook("user:/Documents/report.ods");
sendJSONResp('{"body":' + bodyJson + '}');
.doc, .xls, .ppt)The legacy binary formats are import only: mod/office/doc, xls and
ppt convert a Word, Excel or PowerPoint 97-2003 file (an OLE2 compound
file) into the matching OOXML package, so a legacy file reads exactly as
the .docx / .xlsx / .pptx Office would save from it. The suite never
writes these formats; save the result as the app's own format. Encrypted
(password protected) files and the pre-97 formats (Word 6/95, PowerPoint
95) are refused; Excel 5/95 workbooks open with their values and styles.
What carries over:
HYPERLINK formulas), notes, tab colours and defined names. Charts,
pictures, conditional formats, data validation and VBA are not
converted.office.docToDocument(srcVpath)Read a Word 97-2003 .doc (or .dot) and return the Docs body schema as
a JSON string - the body a .docx of the same document gives.
office.xlsToWorkbook(srcVpath)Read an Excel 97-2003 .xls (or .xlt) and return the Sheets body schema
as a JSON string.
office.pptToPresentation(srcVpath)Read a PowerPoint 97-2003 .ppt (or .pot / .pps) and return the
Slides body schema as a JSON string.
office.convertLegacy(srcVpath, destVpath)Convert a .doc / .xls / .ppt into a real .docx / .xlsx / .pptx
file at destVpath (no envelope is embedded: the result is a plain Office
file). The kind is taken from the source's content, and destVpath must
carry the matching extension (throws otherwise). Needs read access to the
source and write access to the destination. Returns true on success.
requirelib("office");
if (office.convertLegacy("user:/Documents/old report.doc", "user:/Documents/old report.docx")){
sendResp("OK");
}
var bodyJson = office.xlsToWorkbook("user:/Documents/budget.xls");
office.documentToPdf(jsonStr, destVpath)Build a PDF with real, selectable text (not a page raster) from a
serialized Docs body JSON and write it to destVpath. Renders the same
HTML subset as the docx exporter: headings/paragraph styles, inline
bold/italic/underline/color/size/highlight, clickable links, lists, tables with
column widths, cell shading and cell content (block text, lists and
images inside cells), inline data-URL images, explicit page
breaks, page size/orientation/margins, and header/footer text with
optional page numbers. The headings (h1-h6, not the Title paragraph)
become the PDF's outline (bookmarks), nested by level. Core PDF fonts are
Latin-1; characters outside that range are transliterated in the text
(outline titles keep them). Returns true on success.
office.workbookPrintToPdf(printJson, destVpath)Build a real-text PDF from a Sheets print model (not the raw
workbook JSON): {"sheets":[{"name","colW":[px],"rowH":[px],"rows":
[[{"t","b","i","u","fc","bg","al"}]]}]} — formatted display strings
plus print-relevant styles, computed by the web client (which owns
formula evaluation). One A4-landscape section per sheet, columns scaled
down to fit when the sheet is wider than the page; each sheet is an entry
of the PDF's outline (bookmarks). Returns true on success.
requirelib("office");
var ok = office.documentToPdf(bodyJsonString, "user:/Desktop/report.pdf");
if (ok) { sendResp("OK"); }
office.writeBinaryFile(base64, destVpath)Write a file the web client produced. The payload is the file's bytes,
base64 encoded; the same write permission check as every exporter above
applies to destVpath. This exists because some exports can only be
rendered in the browser - the Slides PDF is built there, since only the
browser knows which font it resolved and where every line wrapped.
Returns true on success.
requirelib("office");
var ok = office.writeBinaryFile(base64Payload, "user:/Desktop/deck.pdf");
if (ok) { sendResp("OK"); }