Skip to content

Commit db3ad68

Browse files
authored
Merge pull request googleworkspace#105 from gsuitedevs/docs
Add samples for the Docs advanved service.
2 parents 73146b4 + a77fa1a commit db3ad68

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed

advanced/docs.gs

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/**
2+
* Copyright Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// [START apps_script_docs_create_document]
18+
/**
19+
* Create a new document.
20+
*/
21+
function createDocument() {
22+
var document = Docs.Documents.create({'title': 'My New Document'});
23+
Logger.log('Created document with ID: ' + document.documentId);
24+
}
25+
// [END apps_script_docs_create_document]
26+
27+
// [START apps_script_docs_find_and_replace]
28+
/**
29+
* Performs "replace all".
30+
* @param {string} documentId The document to perform the replace text
31+
* operations on.
32+
* @param {Object} findTextToReplacementMap A map from the "find text" to the
33+
* "replace text".
34+
*/
35+
function findAndReplace(documentId, findTextToReplacementMap) {
36+
var requests = [];
37+
for (var findText in findTextToReplacementMap) {
38+
var replaceText = findTextToReplacementMap[findText];
39+
var request = {
40+
replaceAllText: {
41+
containsText: {
42+
text: findText,
43+
matchCase: true,
44+
},
45+
replaceText: replaceText
46+
}
47+
};
48+
requests.push(request);
49+
}
50+
51+
var response = Docs.Documents.batchUpdate({'requests': requests}, documentId);
52+
var replies = response.replies;
53+
for (var i = 0; i < replies.length; i++) {
54+
var reply = replies[i];
55+
var numReplacements = reply.replaceAllText.occurrencesChanged || 0;
56+
Logger.log('Request %s performed %s replacements.', i, numReplacements);
57+
}
58+
}
59+
// [END apps_script_docs_find_and_replace]
60+
61+
// [START apps_script_docs_insert_and_style_text]
62+
/**
63+
* Insert text at the beginning of the document and then style the inserted
64+
* text.
65+
* @param {string} documentId The document the text is inserted into.
66+
* @param {string} text The text to insert into the document.
67+
*/
68+
function insertAndStyleText(documentId, text) {
69+
var requests = [{
70+
insertText: {
71+
location: {
72+
index: 1
73+
},
74+
text: text
75+
}
76+
},
77+
{
78+
updateTextStyle: {
79+
range: {
80+
startIndex: 1,
81+
endIndex: text.length + 1
82+
},
83+
text_style: {
84+
fontSize: {
85+
magnitude: 12,
86+
unit: 'PT'
87+
},
88+
weightedFontFamily: {
89+
fontFamily: 'Calibri'
90+
}
91+
},
92+
fields: 'weightedFontFamily, fontSize'
93+
}
94+
}];
95+
Docs.Documents.batchUpdate({'requests': requests}, documentId);
96+
}
97+
// [END apps_script_docs_insert_and_style_text]
98+
99+
// [START apps_script_docs_read_first_paragraph]
100+
/**
101+
* Read the first paragraph of the body of a document.
102+
* @param {string} documentId The ID of the document to read.
103+
*/
104+
function readFirstParagraph(documentId) {
105+
var document = Docs.Documents.get(documentId);
106+
var bodyElements = document.body.content;
107+
108+
for (var i = 0; i < bodyElements.length; i++) {
109+
var structuralElement = bodyElements[i];
110+
if (structuralElement.paragraph !== null) {
111+
var paragraphElements = structuralElement.paragraph.elements;
112+
var paragraphText = '';
113+
114+
for (var j = 0; j < paragraphElements.length; j++) {
115+
var paragraphElement = paragraphElements[j];
116+
if (paragraphElement.textRun !== null) {
117+
paragraphText += paragraphElement.textRun.content;
118+
}
119+
}
120+
121+
Logger.log(paragraphText);
122+
return;
123+
}
124+
}
125+
}
126+
// [END apps_script_docs_read_first_paragraph]

docs/quickstart/quickstart.gs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Copyright Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
// [START docs_quickstart]
17+
/**
18+
* Prints the title of the sample document:
19+
* https://docs.google.com/document/d/195j9eDD3ccgjQRttHhJPymLJUCOUjs-jmwTrekvdjFE/edit
20+
*/
21+
function printDocTitle() {
22+
var documentId = '195j9eDD3ccgjQRttHhJPymLJUCOUjs-jmwTrekvdjFE'
23+
var doc = Docs.Documents.get(documentId)
24+
Logger.log('The title of the doc is: %s', doc.title)
25+
}
26+
// [END docs_quickstart]

0 commit comments

Comments
 (0)