Add Ontime XLSX export

This commit is contained in:
Dandaedre 2026-11-03 17:05:31 +01:00
parent c009c5cb38
commit b1ca75a399
7 changed files with 412 additions and 39 deletions

View File

@ -1,15 +1,190 @@
use crate::{
model::{Analysis, Rundown},
model::{Analysis, Entry, Rundown, column_index, column_name},
pipeline::validate,
time::display,
};
use anyhow::{Result, bail, ensure};
use serde_json::{Value, json};
use std::collections::{BTreeMap, BTreeSet};
use std::{
io::Write,
fmt::Write as _,
io::{Cursor, Write},
path::Path,
sync::atomic::{AtomicBool, Ordering},
};
use zip::{ZipWriter, write::SimpleFileOptions};
fn entry_values(entry: &Entry, rundown: &Rundown, usage: &str, column: Option<&str>) -> String {
entry
.source_rows
.iter()
.flat_map(|row| &row.cells)
.filter(|(letter, cell)| {
!cell.text.trim().is_empty()
&& rundown
.mapping
.column_usage
.get(*letter)
.map(String::as_str)
== Some(usage)
&& column.is_none_or(|expected| *letter == expected)
})
.map(|(_, cell)| cell.text.clone())
.collect::<Vec<_>>()
.join("\n")
}
fn metadata_columns(rundown: &Rundown) -> Vec<(String, String)> {
let mut columns = rundown
.mapping
.column_usage
.iter()
.filter(|(_, usage)| usage.as_str() == "metadata")
.map(|(column, _)| {
let label = rundown
.mapping
.column_labels
.get(column)
.cloned()
.unwrap_or_else(|| column.clone());
(column.clone(), label)
})
.collect::<Vec<_>>();
columns.sort_by_key(|(column, _)| column_index(column).unwrap_or(usize::MAX));
let mut used = [
"start",
"end",
"duration",
"cue",
"title",
"note",
"timer type",
]
.into_iter()
.map(str::to_string)
.collect::<BTreeSet<_>>();
for (column, label) in &mut columns {
let base = if label.trim().is_empty() {
format!("Metadata {column}")
} else {
label.trim().to_string()
};
let mut unique = base.clone();
if !used.insert(unique.to_lowercase()) {
unique = format!("{base} ({column})");
used.insert(unique.to_lowercase());
}
*label = unique;
}
columns
}
fn xml_text(value: &str) -> String {
let mut escaped = String::with_capacity(value.len());
for character in value.chars().filter(|character| {
matches!(*character, '\t' | '\n' | '\r')
|| ('\u{20}'..='\u{d7ff}').contains(character)
|| ('\u{e000}'..='\u{fffd}').contains(character)
|| ('\u{10000}'..='\u{10ffff}').contains(character)
}) {
escaped.push_str(match character {
'&' => "&amp;",
'<' => "&lt;",
'>' => "&gt;",
'"' => "&quot;",
'\'' => "&apos;",
_ => {
escaped.push(character);
continue;
}
});
}
escaped
}
fn worksheet(rundown: &Rundown) -> String {
let metadata = metadata_columns(rundown);
let mut rows = vec![vec![
"Start".into(),
"End".into(),
"Duration".into(),
"Cue".into(),
"Title".into(),
"Note".into(),
"Timer type".into(),
]];
rows[0].extend(metadata.iter().map(|(_, label)| label.clone()));
for entry in rundown.entries.iter().filter(|entry| entry.kind == "event") {
let mut row = vec![
display(entry.start_ms),
display(entry.end_ms),
display(entry.duration_ms),
entry.source_rows[0].row.to_string(),
entry.title.clone(),
entry_values(entry, rundown, "notes", None),
"count-down".into(),
];
row.extend(
metadata
.iter()
.map(|(column, _)| entry_values(entry, rundown, "metadata", Some(column))),
);
rows.push(row);
}
let mut xml = String::from(
r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><sheetViews><sheetView workbookViewId="0"><pane ySplit="1" topLeftCell="A2" activePane="bottomLeft" state="frozen"/></sheetView></sheetViews><cols><col min="1" max="3" width="13" customWidth="1"/><col min="4" max="4" width="9" customWidth="1"/><col min="5" max="6" width="34" customWidth="1"/><col min="7" max="64" width="18" customWidth="1"/></cols><sheetData>"#,
);
for (row_index, row) in rows.iter().enumerate() {
let _ = write!(xml, "<row r=\"{}\">", row_index + 1);
for (column, value) in row.iter().enumerate() {
let _ = write!(
xml,
"<c r=\"{}{}\" t=\"inlineStr\"><is><t xml:space=\"preserve\">{}</t></is></c>",
column_name(column),
row_index + 1,
xml_text(value)
);
}
xml.push_str("</row>");
}
xml.push_str("</sheetData><autoFilter ref=\"A1:");
xml.push_str(&column_name(rows[0].len() - 1));
let _ = write!(xml, "{}\"/></worksheet>", rows.len());
xml
}
pub fn xlsx(rundown: &Rundown) -> Result<Vec<u8>> {
ensure!(
validate(rundown).is_empty(),
"Cannot export an inconsistent rundown"
);
let mut zip = ZipWriter::new(Cursor::new(Vec::new()));
let options = SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated);
let sheet = worksheet(rundown);
for (name, contents) in [
(
"[Content_Types].xml",
r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="xml" ContentType="application/xml"/><Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/><Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/></Types>"#,
),
(
"_rels/.rels",
r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/></Relationships>"#,
),
(
"xl/workbook.xml",
r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheets><sheet name="Ontime" sheetId="1" r:id="rId1"/></sheets></workbook>"#,
),
(
"xl/_rels/workbook.xml.rels",
r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/></Relationships>"#,
),
("xl/worksheets/sheet1.xml", sheet.as_str()),
] {
zip.start_file(name, options)?;
zip.write_all(contents.as_bytes())?;
}
Ok(zip.finish()?.into_inner())
}
pub fn ontime(r: &Rundown) -> Result<Value> {
ensure!(
@ -123,18 +298,35 @@ pub fn write(
!output.exists() || overwrite,
"Output exists; use --overwrite or choose another path"
);
let value = if neutral {
serde_json::to_value(r)?
} else {
ontime(r)?
};
let extension = output
.extension()
.and_then(|extension| extension.to_str())
.unwrap_or_default()
.to_ascii_lowercase();
ensure!(
matches!(extension.as_str(), "json" | "xlsx"),
"Output must use .json or .xlsx"
);
ensure!(
!(neutral && extension == "xlsx"),
"Neutral IR export requires a .json output"
);
let parent = output
.parent()
.filter(|p| !p.as_os_str().is_empty())
.unwrap_or(Path::new("."));
let mut temp = tempfile::NamedTempFile::new_in(parent)?;
serde_json::to_writer_pretty(&mut temp, &value)?;
writeln!(temp)?;
if extension == "xlsx" {
temp.write_all(&xlsx(r)?)?;
} else {
let value = if neutral {
serde_json::to_value(r)?
} else {
ontime(r)?
};
serde_json::to_writer_pretty(&mut temp, &value)?;
writeln!(temp)?;
}
temp.as_file().sync_all()?;
if cancel.load(Ordering::Relaxed) {
bail!("Cancelled; no output published");

View File

@ -32,6 +32,36 @@ enum Tab {
Results,
Details,
}
#[derive(Clone, Copy, Default, PartialEq)]
enum ExportChoice {
#[default]
OntimeJson,
OntimeXlsx,
NeutralJson,
}
impl ExportChoice {
fn label(self) -> &'static str {
match self {
Self::OntimeJson => "Ontime project (.json)",
Self::OntimeXlsx => "Ontime spreadsheet (.xlsx)",
Self::NeutralJson => "Neutral intermediate representation (.json)",
}
}
fn extension(self) -> &'static str {
match self {
Self::OntimeJson => "ontime.json",
Self::OntimeXlsx => "ontime.xlsx",
Self::NeutralJson => "neutral.json",
}
}
fn file_extension(self) -> &'static str {
if self == Self::OntimeXlsx {
"xlsx"
} else {
"json"
}
}
}
struct App {
input: String,
output: String,
@ -46,7 +76,7 @@ struct App {
rx: Option<mpsc::Receiver<Update>>,
cancel: Arc<AtomicBool>,
overwrite: bool,
neutral: bool,
export_choice: ExportChoice,
completed: Option<PathBuf>,
settings: bool,
advanced_settings: bool,
@ -105,7 +135,7 @@ impl Default for App {
rx: None,
cancel: Arc::new(AtomicBool::new(false)),
overwrite: false,
neutral: false,
export_choice: ExportChoice::default(),
completed: None,
settings: false,
advanced_settings: false,
@ -149,7 +179,11 @@ impl App {
fn set_input(&mut self, path: PathBuf) {
self.tab = Tab::Setup;
self.input = path.to_string_lossy().into();
self.output = path.with_extension("ontime.json").to_string_lossy().into();
self.export_choice = ExportChoice::OntimeJson;
self.output = path
.with_extension(self.export_choice.extension())
.to_string_lossy()
.into();
self.profile = "{}".into();
self.profile_path = None;
self.sheet.clear();
@ -157,6 +191,18 @@ impl App {
self.tables.clear();
self.invalidate();
}
fn change_export_choice(&mut self, choice: ExportChoice) {
if self.export_choice == choice {
return;
}
self.export_choice = choice;
if !self.input.is_empty() {
self.output = PathBuf::from(&self.input)
.with_extension(choice.extension())
.to_string_lossy()
.into();
}
}
fn begin(&mut self) -> mpsc::Sender<Update> {
let (tx, rx) = mpsc::channel();
self.rx = Some(rx);
@ -219,7 +265,21 @@ impl App {
};
let input = PathBuf::from(&self.input);
let output = PathBuf::from(&self.output);
let neutral = self.neutral;
if !output
.extension()
.and_then(|extension| extension.to_str())
.is_some_and(|extension| {
extension.eq_ignore_ascii_case(self.export_choice.file_extension())
})
{
self.error = Some(format!(
"{} requires a .{} filename",
self.export_choice.label(),
self.export_choice.file_extension()
));
return;
}
let neutral = self.export_choice == ExportChoice::NeutralJson;
let overwrite = self.overwrite;
let tx = self.begin();
let cancel = self.cancel.clone();
@ -665,14 +725,37 @@ impl App {
ui.label("Save to");
ui.add(egui::TextEdit::singleline(&mut self.output).desired_width(f32::INFINITY));
ui.horizontal(|ui| {
let mut choice = self.export_choice;
egui::ComboBox::from_id_salt("export_format")
.selected_text(self.export_choice.label())
.show_ui(ui, |ui| {
for option in [
ExportChoice::OntimeJson,
ExportChoice::OntimeXlsx,
ExportChoice::NeutralJson,
] {
ui.selectable_value(&mut choice, option, option.label());
}
});
self.change_export_choice(choice);
if ui.button("Change…").clicked()
&& let Some(p) = rfd::FileDialog::new()
.set_file_name("rundown.ontime.json")
.save_file()
&& let Some(p) = {
let mut dialog = rfd::FileDialog::new().set_file_name(
PathBuf::from(&self.output)
.file_name()
.unwrap_or_default()
.to_string_lossy(),
);
dialog = if self.export_choice == ExportChoice::OntimeXlsx {
dialog.add_filter("Excel workbook", &["xlsx"])
} else {
dialog.add_filter("JSON", &["json"])
};
dialog.save_file()
}
{
self.output = p.to_string_lossy().into();
}
ui.checkbox(&mut self.neutral, "Neutral JSON instead of Ontime");
});
ui.checkbox(&mut self.overwrite, "Replace existing output");
ui.separator();
@ -736,7 +819,11 @@ impl App {
ui.ctx().copy_text(p.display().to_string());
}
});
ui.label("Import the .ontime.json project in Ontime. Review the schedule before running the show.");
ui.label(if p.extension().and_then(|extension| extension.to_str()) == Some("xlsx") {
"Import the .xlsx file with Ontime's spreadsheet importer and map the displayed columns. Review the schedule before running the show."
} else {
"Import the .ontime.json project in Ontime. Review the schedule before running the show."
});
} else {
ui.label("No export published yet.");
}
@ -986,6 +1073,31 @@ mod tests {
wait(&mut app);
}
#[test]
fn export_choice_updates_default_output_extension() {
let mut app = App::default();
app.set_input(PathBuf::from("show.csv"));
assert!(app.output.ends_with("show.ontime.json"));
app.change_export_choice(ExportChoice::OntimeXlsx);
assert!(app.output.ends_with("show.ontime.xlsx"));
assert_eq!(app.export_choice.label(), "Ontime spreadsheet (.xlsx)");
app.change_export_choice(ExportChoice::NeutralJson);
assert!(app.output.ends_with("show.neutral.json"));
app.output = "show.xlsx".into();
app.analysis = Some(pipeline::analyze(
&reader::read(&fixture()).unwrap()[0],
&Profile::default(),
&AtomicBool::new(false),
|_, _| {},
));
app.export();
assert_eq!(
app.error.as_deref(),
Some("Neutral intermediate representation (.json) requires a .json filename")
);
assert!(app.rx.is_none());
}
#[test]
fn multiple_sheets_focus_the_picker_and_explain_blocked_analysis() {
let mut app = App::default();

View File

@ -12,7 +12,7 @@ enum Action {
#[derive(Parser)]
#[command(
version,
about = "Infer spreadsheet rundown columns, preserve schedule gaps, export Ontime JSON"
about = "Infer spreadsheet rundown columns, preserve schedule gaps, export Ontime JSON or XLSX"
)]
struct Args {
#[arg(value_enum)]
@ -24,6 +24,7 @@ struct Args {
/// JSON profile: explicit columns, units, row classifications and row corrections.
#[arg(long)]
profile: Option<PathBuf>,
/// Destination .json or Ontime-importable .xlsx file.
#[arg(short, long)]
output: Option<PathBuf>,
/// Export the neutral IR instead of an Ontime project.

View File

@ -31,6 +31,16 @@ fn inspect_validate_convert_and_exit_codes() {
.output()
.unwrap();
assert!(result.status.success(), "{:?}", result);
let spreadsheet = dir.path().join("show.xlsx");
let result = cli()
.arg("convert")
.arg(sample())
.arg("--output")
.arg(&spreadsheet)
.output()
.unwrap();
assert!(result.status.success(), "{:?}", result);
assert!(std::fs::read(spreadsheet).unwrap().starts_with(b"PK"));
let result = cli()
.arg("convert")
.arg(sample())

View File

@ -409,6 +409,60 @@ fn atomic_publication_preserves_source_and_existing_output() {
1
);
}
#[test]
fn xlsx_export_has_ontime_columns_notes_metadata_and_clock_strings() {
let source = table(&[
&["Start", "Duration", "Title", "Notes", "Video"],
&["09:00", "5 min", "Opening", "Ready & <go>", "Slides"],
&["09:10", "2 min", "Closing", "", "Camera"],
]);
let profile = Profile {
column_usage: BTreeMap::from([
("D".into(), "notes".into()),
("E".into(), "metadata".into()),
]),
..Profile::default()
};
let analysis = analyze(&source, &profile);
assert!(
analysis.diagnostics.is_empty(),
"{:?}",
analysis.diagnostics
);
let bytes = export::xlsx(analysis.rundown.as_ref().unwrap()).unwrap();
assert!(bytes.starts_with(b"PK"));
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("ontime.xlsx");
std::fs::write(&path, bytes).unwrap();
let tables = reader::read(&path).unwrap();
assert_eq!(tables.len(), 1);
assert_eq!(tables[0].name, "Ontime");
assert_eq!(
tables[0].rows[0]
.iter()
.map(|cell| cell.text.as_str())
.collect::<Vec<_>>(),
[
"Start",
"End",
"Duration",
"Cue",
"Title",
"Note",
"Timer type",
"Video",
]
);
assert_eq!(tables[0].rows[1][0].text, "09:00:00");
assert_eq!(tables[0].rows[1][1].text, "09:05:00");
assert_eq!(tables[0].rows[1][2].text, "00:05:00");
assert_eq!(tables[0].rows[1][5].text, "Ready & <go>");
assert_eq!(tables[0].rows[1][6].text, "count-down");
assert_eq!(tables[0].rows[1][7].text, "Slides");
assert_eq!(tables[0].rows[2][0].text, "09:10:00");
}
#[test]
fn invalid_or_cancelled_analysis_never_exports() {
let a = pipeline::analyze(

View File

@ -178,12 +178,10 @@ published. Keep local source paths out of exported project notes by default.
### Additional exports
Generate a Typst report (editable source and rendered PDF) from the validated IR.
Re-export XLS/ODS spreadsheets with explicit, normalized columns suitable for
Ontime import. Verify accepted file types and exact header/unit requirements
against the target Ontime release before claiming direct import support; if a
requested format needs conversion, document that limitation explicitly. Include
round-trip import tests for timing and gaps. Keep untimed gaps free of timer rows
in every output. These exports do not become intermediate steps for JSON export.
XLSX re-export provides explicit, normalized columns suitable for Ontime import;
ODS remains unavailable because Ontime's current file importer does not expose
ODS upload. Keep untimed gaps free of timer rows in every output. These exports
do not become intermediate steps for JSON export.
### Optional buffer adjustment
@ -301,8 +299,9 @@ errors must remain visible with direct corrective actions.
production GUI coverage and reporter behavior. Record unavailable manual checks.
- [ ] Extend manual packaging/release preparation for both binaries and supported
platforms, including only dependencies this tool needs. CI must not publish releases.
- [ ] Stretch: add Typst source/PDF reports and XLS/ODS re-export with verified
Ontime-compatible columns and documented format support.
- [x] Stretch: add XLSX re-export with verified Ontime-compatible columns and
documented format support.
- [ ] Stretch: add Typst source/PDF reports.
- [ ] Stretch: add explicit buffer adjustment, before/after review, feasibility
checks and revalidation, with the cases listed above.
- [ ] Commit each completed implementation task with its validation summary.

View File

@ -3,7 +3,7 @@
Status: first implementation, available from source. A Rust core powers both the
CLI and egui desktop app, styled like the [PPTX reporter](rust-reporter.md).
It reads XLSX and UTF-8 CSV/TSV, infers columns, preserves untimed gaps and exports
Ontime JSON. Ambiguous documents stop for clarification. See the
Ontime JSON or normalized XLSX. Ambiguous documents stop for clarification. See the
[development plan](rundown-converter-plan.md) and [sample findings](rundown-corpus.md).
## Quick start
@ -15,6 +15,7 @@ cargo run --locked -p rundown-converter --bin rundown-converter-gui
cargo run --locked -p rundown-converter --bin rundown-converter -- inspect show.xlsx
cargo run --locked -p rundown-converter --bin rundown-converter -- validate show.xlsx --sheet "Main"
cargo run --locked -p rundown-converter --bin rundown-converter -- convert show.xlsx --sheet "Main" --output show.ontime.json
cargo run --locked -p rundown-converter --bin rundown-converter -- convert show.xlsx --sheet "Main" --output show.ontime.xlsx
```
Try the included synthetic lunch example:
@ -26,8 +27,9 @@ cargo run --locked -p rundown-converter --bin rundown-converter -- convert crate
Select a sheet explicitly when a workbook has several. `inspect --json` exposes
original cells, formulas, formats relevant to timing, and merged ranges.
`validate --json` gives structured decisions, diagnostics and the IR when available.
`convert --neutral` exports that neutral IR instead of Ontime JSON. Existing output
requires `--overwrite`; the source itself cannot be the output. Parent output
The output extension selects native `.json` or spreadsheet `.xlsx` export.
`convert --neutral` exports the neutral IR to JSON instead of an Ontime project.
Existing output requires `--overwrite`; the source itself cannot be the output. Parent output
directories must already exist. Exit codes: 0 success, 1 read/profile/write failure,
2 unresolved rundown diagnostics (also used by clap for invalid command syntax).
CLI logs are plain text on stderr; JSON results go to stdout.
@ -169,8 +171,9 @@ Open or drop a document and select its sheet/table, then follow five stages:
and inferred values. Ask for clarification when required facts remain unknown.
4. Check the complete schedule for inconsistencies, including backward times,
negative durations, overlaps and contradictory start/end/duration values.
5. Generate native Ontime JSON from the validated IR, with optional neutral JSON
for inspection or reuse. Open the result or its output folder.
5. Generate native Ontime JSON or an Ontime-ready XLSX from the validated IR,
with optional neutral JSON for inspection or reuse. Open the result or its
output folder.
Review and correction controls expose each stage's decisions and errors. CLI and
GUI follow this same sequence; export cannot bypass unresolved errors.
@ -225,13 +228,15 @@ operator will start an event on time.
Native Ontime JSON is the primary target: it lets the exporter control time
relationships and project structure without a second column-mapping step.
Ontime supports project files and spreadsheet interchange; see its
[import/export documentation](https://docs.getontime.no/quick-tips/import-export/).
Stretch goals are a Typst report (editable source and PDF) and XLS/ODS re-export
with normalized columns directly suitable for Ontime import. Actual file-format
and column compatibility must be verified against the target release; any required
format conversion will be documented. These outputs share the validated IR and
are not intermediate steps for native JSON export.
XLSX export writes one `Ontime` worksheet with `Start`, `End`, `Duration`, `Cue`,
`Title`, `Note` and `Timer type` columns, followed by selected metadata columns.
Times use `HH:MM:SS` strings accepted by Ontime. Import the file with Ontime's
[spreadsheet importer](https://docs.getontime.no/features/import-spreadsheet/)
and map the displayed columns. Schedule gaps remain visible through absolute
event starts; the exporter does not invent timer rows for them. Ontime's current
file importer accepts XLSX and does not expose ODS upload, so ODS is not generated.
These outputs share the validated IR and are not intermediate steps for native
JSON export. A Typst source/PDF report remains a stretch goal.
The adapter targets Ontime 4 native project JSON. Its events and schedule metadata
were checked with the Ontime 4.15.1 native parser; see the