Skip to content

Commit e498743

Browse files
fix: Update docs.gs for the tabs in Docs feature (#479)
This is a part of an ongoing effort to update Docs API guides/documentation for the tabs feature.
1 parent 2e6ee8b commit e498743

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

advanced/docs.gs

+24-6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ function findAndReplace(documentId, findTextToReplacementMap) {
4545
const requests = [];
4646
for (const findText in findTextToReplacementMap) {
4747
const replaceText = findTextToReplacementMap[findText];
48+
// One option for replacing all text is to specify all tab IDs.
49+
const request = {
50+
replaceAllText: {
51+
containsText: {
52+
text: findText,
53+
matchCase: true
54+
},
55+
replaceText: replaceText,
56+
tabsCriteria: {
57+
tabIds: [TAB_ID_1, TAB_ID_2, TAB_ID_3],
58+
}
59+
}
60+
};
61+
// Another option is to omit TabsCriteria if you are replacing across all tabs.
4862
const request = {
4963
replaceAllText: {
5064
containsText: {
@@ -73,8 +87,8 @@ function findAndReplace(documentId, findTextToReplacementMap) {
7387

7488
// [START docs_insert_and_style_text]
7589
/**
76-
* Insert text at the beginning of the document and then style the inserted
77-
* text.
90+
* Insert text at the beginning of the first tab in the document and then style
91+
* the inserted text.
7892
* @param {string} documentId The document the text is inserted into.
7993
* @param {string} text The text to insert into the document.
8094
* @return {Object} replies
@@ -84,7 +98,10 @@ function insertAndStyleText(documentId, text) {
8498
const requests = [{
8599
insertText: {
86100
location: {
87-
index: 1
101+
index: 1,
102+
// A tab can be specified using its ID. When omitted, the request is
103+
// applied to the first tab.
104+
// tabId: TAB_ID
88105
},
89106
text: text
90107
}
@@ -119,16 +136,17 @@ function insertAndStyleText(documentId, text) {
119136

120137
// [START docs_read_first_paragraph]
121138
/**
122-
* Read the first paragraph of the body of a document.
139+
* Read the first paragraph of the first tab in a document.
123140
* @param {string} documentId The ID of the document to read.
124141
* @return {Object} paragraphText
125142
* @see https://developers.google.com/docs/api/reference/rest/v1/documents/get
126143
*/
127144
function readFirstParagraph(documentId) {
128145
try {
129146
// Get the document using document ID
130-
const document = Docs.Documents.get(documentId);
131-
const bodyElements = document.body.content;
147+
const document = Docs.Documents.get({'includeTabsContent': true}, documentId);
148+
const firstTab = document.tabs[0];
149+
const bodyElements = firstTab.documentTab.body.content;
132150
for (let i = 0; i < bodyElements.length; i++) {
133151
const structuralElement = bodyElements[i];
134152
// Print the first paragraph text present in document

0 commit comments

Comments
 (0)