コード例 #1
0
 public static int Handle(SpreadsheetImportParameters options)
 {
     try
     {
         string folderPath = Directory.GetParent(options.BookPath).FullName;
         BookStorage.SaveCopyBeforeImportOverwrite(folderPath);
         var sheet    = InternalSpreadsheet.ReadFromFile(options.InputPath);
         var dom      = new HtmlDom(XmlHtmlConverter.GetXmlDomFromHtmlFile(options.BookPath, false));
         var importer = new SpreadsheetImporter(null, dom, Path.GetDirectoryName(options.InputPath), folderPath);
         if (!string.IsNullOrEmpty(options.ParamsPath))
         {
             importer.Params = SpreadsheetImportParams.FromFile(options.ParamsPath);
         }
         var messages = importer.Import(sheet);
         foreach (var message in messages)
         {
             Debug.WriteLine(message);
             Console.WriteLine(message);
         }
         // Review: A lot of other stuff happens in Book.Save() and BookStorage.SaveHtml().
         // I doubt we need any of it for current purposes, but later we might.
         XmlHtmlConverter.SaveDOMAsHtml5(dom.RawDom, options.BookPath);
         Console.WriteLine("done");
         return(0);                // all went well
     }
     catch (Exception ex)
     {
         Debug.WriteLine(ex.Message);
         Console.WriteLine(ex.Message);
         Console.WriteLine(ex.StackTrace);
         return(1);
     }
 }
コード例 #2
0
        public void ImportWithProgress(string inputFilepath)
        {
            Debug.Assert(_pathToBookFolder != null,
                         "Somehow we made it into ImportWithProgress() without a path to the book folder");
            var mainShell = Application.OpenForms.Cast <Form>().FirstOrDefault(f => f is Shell);

            BrowserProgressDialog.DoWorkWithProgressDialog(_webSocketServer, "spreadsheet-import", () =>
                                                           new ReactDialog("progressDialogBundle",
                                                                           // props to send to the react component
                                                                           new
            {
                title                = "Importing Spreadsheet",
                titleIcon            = "",                              // enhance: add icon if wanted
                titleColor           = "white",
                titleBackgroundColor = Palette.kBloomBlueHex,
                webSocketContext     = "spreadsheet-import",
                showReportButton     = "if-error"
            }, "Import Spreadsheet")
                                                           // winforms dialog properties
            {
                Width = 620, Height = 550
            }, (progress, worker) =>
            {
                var hasAudio           = _destinationDom.GetRecordedAudioSentences(_pathToBookFolder).Any();
                var cannotImportEnding = " For this reason, we need to abandon the import. Instead, you can import into a blank book.";
                if (hasAudio)
                {
                    progress.MessageWithoutLocalizing($"Warning: Spreadsheet import cannot currently preserve Talking Book audio that is already in this book." + cannotImportEnding, ProgressKind.Error);
                    return(true);                    // leave progress window up so user can see error.
                }
                var hasActivities = _destinationDom.HasActivityPages();
                if (hasActivities)
                {
                    progress.MessageWithoutLocalizing($"Warning: Spreadsheet import cannot currently preserve quizzes, widgets, or other activities that are already in this book." + cannotImportEnding, ProgressKind.Error);
                    return(true);                    // leave progress window up so user can see error.
                }
                var sheet = InternalSpreadsheet.ReadFromFile(inputFilepath, progress);
                if (sheet == null)
                {
                    return(true);
                }
                if (!Validate(sheet, progress))
                {
                    return(true);                    // errors already reported to progress
                }
                progress.MessageWithoutLocalizing($"Making a backup of the original book...");
                var backupPath = BookStorage.SaveCopyBeforeImportOverwrite(_pathToBookFolder);
                progress.MessageWithoutLocalizing($"Backup completed (at {backupPath})");
                Import(sheet, progress);
                return(true);                // always leave the dialog up until the user chooses 'close'
            }, null, mainShell);
        }