Esempio n. 1
0
        public IEnumerable <IQuestion> Convert()
        {
            var sheet = _workbook.GetSheet(_reportSheetName);

            IRow lastRow   = null;
            var  lastRowId = "";

            // Loop through each row (skipping first row).
            for (var rowNum = 1; rowNum <= sheet.LastRowNum; rowNum++)
            {
                var row = sheet.GetRow(rowNum);

                var currentRowId = row.GetCell(0)?.StringCellValue;

                if (currentRowId != null &&
                    !currentRowId.Equals(lastRowId, StringComparison.InvariantCultureIgnoreCase))
                {
                    if (lastRow != null)
                    {
                        // Create question from last row.
                        yield return(QuestionFactory.Build(lastRow));
                    }

                    // Set new last row.
                    lastRow   = row;
                    lastRowId = currentRowId;
                }
            }

            // Handle last row
            yield return(QuestionFactory.Build(lastRow));
        }
Esempio n. 2
0
        /// <inheritdoc />
        /// <exception cref="IOException">Thrown if there is an error opening or reading the source file.</exception>
        public IEnumerable <IQuestion> Convert()
        {
            var fileStream = File.OpenRead(_sourceFile);
            var doc        = new XWPFDocument(fileStream);

            var questions = 0;

            Question currentQuestion   = null;
            var      inQuestion        = false;
            var      answers           = 1;
            var      skippedPageBreaks = 0;

            foreach (var paragraph in doc.Paragraphs)
            {
                // Skip # of pages
                if (skippedPageBreaks < _skipPageBreaks)
                {
                    // paragraph.IsPageBreak is useless, need to find break in runs.
                    if (paragraph.Runs.Select(textRun => textRun.GetCTR()).Any(ctr => ctr.SizeOfBrArray() > 0))
                    {
                        questions       = 0;
                        currentQuestion = null;
                        skippedPageBreaks++;
                    }
                }

                var numFormat = paragraph.GetNumFmt();

                // Check are we at a new question or answer?
                switch (numFormat)
                {
                case "decimal":     // Decimal ordered list means new question
                {
                    inQuestion = true;

                    if (currentQuestion != null && skippedPageBreaks == _skipPageBreaks)
                    {
                        yield return(QuestionFactory.Build(currentQuestion));
                    }

                    // Reset variables
                    currentQuestion = new Question(++questions, paragraph.Text);

                    answers = 0;
                    break;
                }

                case "lowerLetter":     // Letter ordered list means new answer.
                {
                    currentQuestion?.Answers.Add(paragraph.Text);

                    // Check if answer is answer color
                    var color = DetermineParagraphColor(paragraph);

                    if (color.HasValue && currentQuestion != null)
                    {
                        if (IsAnswer(paragraph))
                        {
                            // This is the answer.
                            currentQuestion.CorrectAnswerIndex = answers;
                        }
                    }

                    inQuestion = false;
                    answers++;

                    break;
                }

                default:     // More text in question or answer.
                {
                    if (inQuestion && currentQuestion != null)
                    {
                        if (IsAnswer(paragraph))
                        {
                            inQuestion = false;

                            currentQuestion.Answers.Add(paragraph.Text);
                        }
                        else
                        {
                            currentQuestion.QuestionText += paragraph.Text;
                        }
                    }
                    else if (currentQuestion != null && currentQuestion.Answers.Count > 0)
                    {
                        currentQuestion.Answers[currentQuestion.Answers.Count - 1] += paragraph.Text;
                    }

                    break;
                }
                }
            }

            // Get last remaining question.
            if (currentQuestion != null)
            {
                yield return(QuestionFactory.Build(currentQuestion));
            }
        }