static int RunOptions(Options opts) { try { OpenXmlPackage package; if (!File.Exists(opts.Input)) { Console.Error.WriteLine("[Error] Specify -i,--input."); return(1); } var documentType = Path.GetExtension(opts.Input); switch (documentType) { case ".docx": case ".docm": case ".dotm": case ".dotx": using (var document = WordprocessingDocument.Open(opts.Input, false)) { package = document.Clone(); } break; case ".xlsx": case ".xlsm": case ".xltm": case ".xltx": using (var document = SpreadsheetDocument.Open(opts.Input, false)) { package = document.Clone(); } break; default: throw new OpenXmlPackageException(); } if (opts.Inspect) { return(RunInspect(package)); } if (File.Exists(opts.Metadata)) { var obj = JObject.Parse(File.ReadAllText(opts.Metadata)); Utils.ModifyMetadata(package, obj); } if (!string.IsNullOrEmpty(opts.Url)) { string name = package.GetType().Name; // TODO: Is it possible to inject templates into xlsx? if (opts.Template) { if (name == "WordprocessingDocument") { WordprocessingDocument document = (WordprocessingDocument)package; document.InsertTemplateURI(opts.Url); } else { Console.Error.WriteLine("[Error] Not supported."); return(1); } } else { if (name == "WordprocessingDocument") { WordprocessingDocument document = (WordprocessingDocument)package; document.InsertTrackingURI(opts.Url); } else if (name == "SpreadsheetDocument") { SpreadsheetDocument workbook = (SpreadsheetDocument)package; workbook.InsertTrackingURI(opts.Url); } } } if (string.IsNullOrEmpty(opts.Output)) { Console.Error.WriteLine("[Error] Specify -o, --output."); return(1); } package.SaveAs(opts.Output); } catch (OpenXmlPackageException) { Console.Error.WriteLine("[Error] Document type mismatch. Provide correct Office Open XML file."); return(1); } catch (Exception e) { Console.Error.WriteLine("[Error] {0}", e.Message); return(1); } return(0); }
static int RunOptions(Options opts) { if (opts.ListTypes) { return(RunListTypes()); } try { OpenXmlPackage package; if (string.IsNullOrEmpty(opts.Input)) { Console.Error.WriteLine("#TODO"); return(1); } if (string.IsNullOrEmpty(opts.Type)) { Console.Error.WriteLine("[Error] Specify -t, --type. Just use either Document or Workbook if you want to insert URLs."); return(1); } switch (opts.Type) { case "Document": case "MacroEnabledDocument": case "MacroEnabledTemplate": case "Template": using (var document = WordprocessingDocument.Open(opts.Input, false)) { package = document.Clone(); } break; case "Workbook": case "MacroEnabledWorkbook": case "MacroEnabledTemplateX": case "TemplateX": using (var document = SpreadsheetDocument.Open(opts.Input, false)) { package = document.Clone(); } break; default: Console.Error.WriteLine("[Error] Specify correct document type, use --list-types to view types."); return(1); } if (opts.Inspect) { return(RunInspect(package)); } if (File.Exists(opts.Metadata)) { var obj = JObject.Parse(File.ReadAllText(opts.Metadata)); Utils.ModifyMetadata(package, obj); } if (!string.IsNullOrEmpty(opts.Url)) { if (opts.Template) { WordprocessingDocument document = (WordprocessingDocument)package; document.InsertTemplateURI(opts.Url); } else { string name = package.GetType().Name; if (name == "WordprocessingDocument") { WordprocessingDocument document = (WordprocessingDocument)package; document.InsertTrackingURI(opts.Url); } else if (name == "SpreadsheetDocument") { SpreadsheetDocument workbook = (SpreadsheetDocument)package; workbook.InsertTrackingURI(opts.Url); } } } if (string.IsNullOrEmpty(opts.Output)) { Console.Error.WriteLine("[Error] Specify -o, --output."); return(1); } package.SaveAs(opts.Output); } catch (OpenXmlPackageException) { Console.Error.WriteLine("[Error] Document type mismatch."); return(1); } catch (Exception e) { Console.Error.WriteLine("[Error] {0}", e.Message); return(1); } return(0); }