2121 * @return {string } documentId
2222 */
2323function createDocument ( ) {
24- try {
25- // Create document with title
26- const document = Docs . Documents . create ( { title : "My New Document" } ) ;
27- console . log ( `Created document with ID: ${ document . documentId } ` ) ;
28- return document . documentId ;
29- } catch ( e ) {
30- // TODO (developer) - Handle exception
31- console . log ( "Failed with error %s" , e . message ) ;
32- }
24+ // Create document with title
25+ const document = Docs . Documents . create ( { title : "My New Document" } ) ;
26+ console . log ( `Created document with ID: ${ document . documentId } ` ) ;
27+ return document . documentId ;
3328}
3429// [END docs_create_document]
3530
@@ -45,51 +40,44 @@ function findAndReplace(documentId, findTextToReplacementMap) {
4540 const requests = [ ] ;
4641 for ( const findText in findTextToReplacementMap ) {
4742 const replaceText = findTextToReplacementMap [ findText ] ;
48- // One option for replacing all text is to specify all tab IDs.
49- const request = {
43+
44+ // Replace all text across all tabs.
45+ const replaceAllTextRequest = {
5046 replaceAllText : {
5147 containsText : {
5248 text : findText ,
5349 matchCase : true ,
5450 } ,
5551 replaceText : replaceText ,
56- tabsCriteria : {
57- tabIds : [ TAB_ID_1 , TAB_ID_2 , TAB_ID_3 ] ,
58- } ,
5952 } ,
6053 } ;
61- // Another option is to omit TabsCriteria if you are replacing across all tabs.
62- const request = {
54+
55+ // Replace all text across specific tabs.
56+ const _replaceAllTextWithTabsCriteria = {
6357 replaceAllText : {
64- containsText : {
65- text : findText ,
66- matchCase : true ,
58+ ... replaceAllTextRequest . replaceAllText ,
59+ tabsCriteria : {
60+ tabIds : [ TAB_ID_1 , TAB_ID_2 , TAB_ID_3 ] ,
6761 } ,
68- replaceText : replaceText ,
6962 } ,
7063 } ;
71- requests . push ( request ) ;
64+ requests . push ( replaceAllTextRequest ) ;
7265 }
73- try {
74- const response = Docs . Documents . batchUpdate (
75- { requests : requests } ,
76- documentId ,
66+ const response = Docs . Documents . batchUpdate (
67+ { requests : requests } ,
68+ documentId ,
69+ ) ;
70+ const replies = response . replies ;
71+ for ( const [ index ] of replies . entries ( ) ) {
72+ const numReplacements =
73+ replies [ index ] . replaceAllText . occurrencesChanged || 0 ;
74+ console . log (
75+ "Request %s performed %s replacements." ,
76+ index ,
77+ numReplacements ,
7778 ) ;
78- const replies = response . replies ;
79- for ( const [ index ] of replies . entries ( ) ) {
80- const numReplacements =
81- replies [ index ] . replaceAllText . occurrencesChanged || 0 ;
82- console . log (
83- "Request %s performed %s replacements." ,
84- index ,
85- numReplacements ,
86- ) ;
87- }
88- return replies ;
89- } catch ( e ) {
90- // TODO (developer) - Handle exception
91- console . log ( "Failed with error : %s" , e . message ) ;
9279 }
80+ return replies ;
9381}
9482// [END docs_find_and_replace_text]
9583
@@ -134,16 +122,11 @@ function insertAndStyleText(documentId, text) {
134122 } ,
135123 } ,
136124 ] ;
137- try {
138- const response = Docs . Documents . batchUpdate (
139- { requests : requests } ,
140- documentId ,
141- ) ;
142- return response . replies ;
143- } catch ( e ) {
144- // TODO (developer) - Handle exception
145- console . log ( "Failed with an error %s" , e . message ) ;
146- }
125+ const response = Docs . Documents . batchUpdate (
126+ { requests : requests } ,
127+ documentId ,
128+ ) ;
129+ return response . replies ;
147130}
148131// [END docs_insert_and_style_text]
149132
@@ -155,33 +138,28 @@ function insertAndStyleText(documentId, text) {
155138 * @see https://developers.google.com/docs/api/reference/rest/v1/documents/get
156139 */
157140function readFirstParagraph ( documentId ) {
158- try {
159- // Get the document using document ID
160- const document = Docs . Documents . get ( documentId , {
161- includeTabsContent : true ,
162- } ) ;
163- const firstTab = document . tabs [ 0 ] ;
164- const bodyElements = firstTab . documentTab . body . content ;
165- for ( let i = 0 ; i < bodyElements . length ; i ++ ) {
166- const structuralElement = bodyElements [ i ] ;
167- // Print the first paragraph text present in document
168- if ( structuralElement . paragraph ) {
169- const paragraphElements = structuralElement . paragraph . elements ;
170- let paragraphText = "" ;
141+ // Get the document using document ID
142+ const document = Docs . Documents . get ( documentId , {
143+ includeTabsContent : true ,
144+ } ) ;
145+ const firstTab = document . tabs [ 0 ] ;
146+ const bodyElements = firstTab . documentTab . body . content ;
147+ for ( let i = 0 ; i < bodyElements . length ; i ++ ) {
148+ const structuralElement = bodyElements [ i ] ;
149+ // Print the first paragraph text present in document
150+ if ( structuralElement . paragraph ) {
151+ const paragraphElements = structuralElement . paragraph . elements ;
152+ let paragraphText = "" ;
171153
172- for ( let j = 0 ; j < paragraphElements . length ; j ++ ) {
173- const paragraphElement = paragraphElements [ j ] ;
174- if ( paragraphElement . textRun !== null ) {
175- paragraphText += paragraphElement . textRun . content ;
176- }
154+ for ( let j = 0 ; j < paragraphElements . length ; j ++ ) {
155+ const paragraphElement = paragraphElements [ j ] ;
156+ if ( paragraphElement . textRun !== null ) {
157+ paragraphText += paragraphElement . textRun . content ;
177158 }
178- console . log ( paragraphText ) ;
179- return paragraphText ;
180159 }
160+ console . log ( paragraphText ) ;
161+ return paragraphText ;
181162 }
182- } catch ( e ) {
183- // TODO (developer) - Handle exception
184- console . log ( "Failed with error %s" , e . message ) ;
185163 }
186164}
187165// [END docs_read_first_paragraph]
0 commit comments