示例#1
0
        private void AddHeaderLine(string text, List <Request> requests, int header, bool newline = true)
        {
            if (newline)
            {
                AddTextLine(text, requests);
            }
            else
            {
                AddText(text, requests);
            }

            Request request = new Request();
            UpdateParagraphStyleRequest updateParagraphStyleRequest = new UpdateParagraphStyleRequest();

            Google.Apis.Docs.v1.Data.Range range = new Google.Apis.Docs.v1.Data.Range();

            if (newline)
            {
                range.StartIndex = CurrentIndex - 1 - text.Length;
            }
            else
            {
                range.StartIndex = CurrentIndex - text.Length;
            }
            range.EndIndex = CurrentIndex;
            ParagraphStyle paragraphStyle = new ParagraphStyle();

            switch (header)
            {
            case 1:
                paragraphStyle.NamedStyleType = "HEADING_1";
                break;

            case 2:
                paragraphStyle.NamedStyleType = "HEADING_2";
                break;

            case 3:
                paragraphStyle.NamedStyleType = "HEADING_3";
                break;

            default:
                throw new Exception("Invalid header");
            }

            updateParagraphStyleRequest.Range          = range;
            updateParagraphStyleRequest.ParagraphStyle = paragraphStyle;
            updateParagraphStyleRequest.Fields         = "*";
            request.UpdateParagraphStyle = updateParagraphStyleRequest;

            requests.Add(request);
        }
示例#2
0
        private void Underline(int startIndex, int endIndex, List <Request> requests)
        {
            Request request = new Request();

            UpdateTextStyleRequest updateTextStyleRequest = new UpdateTextStyleRequest();
            TextStyle textStyle = new TextStyle();

            textStyle.Underline = true;
            updateTextStyleRequest.TextStyle = textStyle;
            updateTextStyleRequest.Fields    = "*";

            Google.Apis.Docs.v1.Data.Range range = new Google.Apis.Docs.v1.Data.Range();
            range.StartIndex = startIndex;
            range.EndIndex   = endIndex;

            updateTextStyleRequest.Range = range;
            request.UpdateTextStyle      = updateTextStyleRequest;

            requests.Add(request);
        }
示例#3
0
        private void AddParagraph(string text, List <Request> requests)
        {
            AddTextLine(text, requests);

            Request request = new Request();
            UpdateParagraphStyleRequest updateParagraphStyleRequest = new UpdateParagraphStyleRequest();

            Google.Apis.Docs.v1.Data.Range range = new Google.Apis.Docs.v1.Data.Range();
            range.StartIndex = CurrentIndex - 1 - text.Length;
            range.EndIndex   = CurrentIndex;
            ParagraphStyle paragraphStyle = new ParagraphStyle();

            paragraphStyle.NamedStyleType = "NORMAL_TEXT";

            updateParagraphStyleRequest.Range          = range;
            updateParagraphStyleRequest.ParagraphStyle = paragraphStyle;
            updateParagraphStyleRequest.Fields         = "*";
            request.UpdateParagraphStyle = updateParagraphStyleRequest;

            requests.Add(request);
        }
示例#4
0
        private void DeleteAllContent(List <Request> requests, Document doc, DocsService service, string documentId)
        {
            DeleteContentRangeRequest deleteContentRangeRequest = new DeleteContentRangeRequest();

            Google.Apis.Docs.v1.Data.Range range = new Google.Apis.Docs.v1.Data.Range();
            range.StartIndex = 1;
            range.EndIndex   = 1000000000;

            deleteContentRangeRequest.Range = range;

            Request request = new Request();

            request.DeleteContentRange = deleteContentRangeRequest;

            try {
                BatchUpdateDocumentRequest tryBody = new BatchUpdateDocumentRequest();

                tryBody.Requests = new List <Request> {
                    request
                };
                DocumentsResource.BatchUpdateRequest tryUpdateRequest = service.Documents.BatchUpdate(tryBody, documentId);

                BatchUpdateDocumentResponse tryResponse = tryUpdateRequest.Execute();
            } catch (Exception e) {
                string[] responseData = e.Message.Split("must be less than the end index of the referenced segment, ");
                responseData = responseData[1].Split(".");

                range.EndIndex = Int32.Parse(responseData[0]) - 1;

                if (range.EndIndex - range.StartIndex != 0)
                {
                    deleteContentRangeRequest.Range = range;

                    request = new Request();
                    request.DeleteContentRange = deleteContentRangeRequest;
                    requests.Add(request);
                }
            }
        }
示例#5
0
        private void AddQuizQuestions(QuizLevel quizLevel, List <Request> requests)
        {
            Request request = new Request();

            InsertTableRequest insertTableRequest = new InsertTableRequest();

            insertTableRequest.Columns = 2;
            insertTableRequest.Rows    = quizLevel.questions.Count;
            insertTableRequest.EndOfSegmentLocation = new EndOfSegmentLocation();

            request.InsertTable = insertTableRequest;

            requests.Add(request);

            int insertIndex = CurrentIndex;

            insertIndex += 4;

            foreach (Question question in quizLevel.questions)
            {
                AddText(question.question, requests, insertIndex);
                insertIndex += question.question.Length;
                insertIndex += 2;

                CreateParagraphBulletsRequest createParagraphBulletsRequest = new CreateParagraphBulletsRequest();
                createParagraphBulletsRequest.BulletPreset = "BULLET_DISC_CIRCLE_SQUARE";
                Google.Apis.Docs.v1.Data.Range range = new Google.Apis.Docs.v1.Data.Range();
                range.StartIndex = insertIndex;

                int n = question.possibleAnswers.Count;
                for (int i = 0; i < n; i++)
                {
                    string text = question.possibleAnswers[i].answer;
                    if (i != n - 1)
                    {
                        text += "\n";
                    }
                    AddText(text, requests, insertIndex);

                    if (question.possibleAnswers[i].answer == question.correct.answer)
                    {
                        Underline(insertIndex, insertIndex + question.possibleAnswers[i].answer.Length, requests);
                    }

                    insertIndex += text.Length;
                }

                range.EndIndex = insertIndex;
                createParagraphBulletsRequest.Range = range;
                request = new Request();
                request.CreateParagraphBullets = createParagraphBulletsRequest;
                requests.Add(request);

                insertIndex += 3;
            }


            CurrentIndex += 6;
            CurrentIndex += quizLevel.questions.Count * 3;
            CurrentIndex += (quizLevel.questions.Count - 1) * 2;
        }