Skip to content

Commit fdc6d6c

Browse files
committed
style: manual fixes
1 parent 791d72c commit fdc6d6c

File tree

36 files changed

+385
-390
lines changed

36 files changed

+385
-390
lines changed

GEMINI.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This guide outlines best practices for developing Google Apps Script projects, f
66

77
* For new sample directories, ensure the top-level folder is included in the [`test.yaml`](.github/workflows/test.yaml) GitHub workflow's matrix configuration.
88
* Do not move or delete snippet tags: `[END apps_script_... ]` or `[END apps_script_... ]`.
9+
* Keep code within snippet tags self-contained. Avoid depending on helper functions defined outside the snippet tags if the snippet is intended to be copied and pasted.
910

1011

1112
## Tools

advanced/chat.gs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ function listMemberships(spaceName) {
105105
pageToken = response.nextPageToken;
106106
continue;
107107
}
108-
response.memberships.forEach((membership) =>
108+
for (const membership of response.memberships) {
109109
console.log(
110-
"Member resource name: %s (type: %s)",
111-
membership.name,
112-
membership.member.type,
113-
),
114-
);
110+
"Member: %s, Role: %s",
111+
membership.member.displayName,
112+
membership.role,
113+
);
114+
}
115115
pageToken = response.nextPageToken;
116116
} while (pageToken);
117117
} catch (err) {

advanced/docs.gs

Lines changed: 50 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,10 @@
2121
* @return {string} documentId
2222
*/
2323
function 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
*/
157140
function 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]

advanced/driveLabels.gs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function listLabelsOnDriveItem(fileId) {
7878
appliedLabels.labels.length,
7979
);
8080

81-
appliedLabels.labels.forEach((appliedLabel) => {
81+
for (const appliedLabel of appliedLabels.labels) {
8282
// Resource name of the label at the applied revision.
8383
const labelName = `labels/${appliedLabel.id}@${appliedLabel.revisionId}`;
8484

@@ -89,7 +89,7 @@ function listLabelsOnDriveItem(fileId) {
8989

9090
console.log("Label Title: %s", label.properties.title);
9191

92-
Object.keys(appliedLabel.fields).forEach((fieldId) => {
92+
for (const fieldId of Object.keys(appliedLabel.fields)) {
9393
const fieldValue = appliedLabel.fields[fieldId];
9494
const field = label.fields.find((f) => f.id === fieldId);
9595

@@ -133,8 +133,8 @@ function listLabelsOnDriveItem(fileId) {
133133
console.log("Unknown: %s", fieldValue.valueType);
134134
console.log(fieldValue.value);
135135
}
136-
});
137-
});
136+
}
137+
}
138138
} catch (err) {
139139
// TODO (developer) - Handle exception
140140
console.log("Failed with error %s", err.message);

advanced/gmail.gs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ function listInboxSnippets() {
4545
pageToken: pageToken,
4646
});
4747
if (threadList.threads && threadList.threads.length > 0) {
48-
threadList.threads.forEach((thread) => {
49-
console.log("Snippet: %s", thread.snippet);
50-
});
48+
for (const thread of threadList.threads) {
49+
console.log("Thread ID: %s", thread.id);
50+
}
5151
}
5252
pageToken = threadList.nextPageToken;
5353
} while (pageToken);
@@ -88,20 +88,20 @@ function logRecentHistory() {
8888
});
8989
const history = recordList.history;
9090
if (history && history.length > 0) {
91-
history.forEach((record) => {
92-
record.messages.forEach((message) => {
91+
for (const record of history) {
92+
for (const message of record.messages) {
9393
if (changed.indexOf(message.id) === -1) {
9494
changed.push(message.id);
9595
}
96-
});
97-
});
96+
}
97+
}
9898
}
9999
pageToken = recordList.nextPageToken;
100100
} while (pageToken);
101101

102-
changed.forEach((id) => {
102+
for (const id of changed) {
103103
console.log("Message Changed: %s", id);
104-
});
104+
}
105105
} catch (err) {
106106
console.log(err);
107107
}

advanced/iot.gs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ function listRegistries() {
2525
const response = CloudIoT.Projects.Locations.Registries.list(parent);
2626
console.log(response);
2727
if (response.deviceRegistries) {
28-
response.deviceRegistries.forEach((registry) => {
28+
for (const registry of response.deviceRegistries) {
2929
console.log(registry.id);
30-
});
30+
}
3131
}
3232
}
3333
// [END apps_script_iot_list_registries]
@@ -115,9 +115,9 @@ function listDevicesForRegistry() {
115115

116116
console.log("Registry contains the following devices: ");
117117
if (response.devices) {
118-
response.devices.forEach((device) => {
118+
for (const device of response.devices) {
119119
console.log(`\t${device.id}`);
120-
});
120+
}
121121
}
122122
}
123123
// [END apps_script_iot_list_devices]

advanced/tagManager.gs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,6 @@ function createContainerVersion(accountPath) {
8383
// [END apps_script_tag_manager_create_version]
8484

8585
// [START apps_script_tag_manager_publish_version]
86-
/**
87-
* Retrieves the container path from a container version path.
88-
* @param {string} versionPath The version path.
89-
* @return {string} The container path.
90-
*/
91-
function grabContainerPath(versionPath) {
92-
const pathParts = versionPath.split("/");
93-
return pathParts.slice(0, 4).join("/");
94-
}
9586

9687
/**
9788
* Publishes a container version publically to the world and creates a quick
@@ -100,7 +91,8 @@ function grabContainerPath(versionPath) {
10091
*/
10192
function publishVersionAndQuickPreviewDraft(version) {
10293
try {
103-
const containerPath = grabContainerPath(version.path);
94+
const pathParts = version.path.split("/");
95+
const containerPath = pathParts.slice(0, 4).join("/");
10496
// Publish the input container version.
10597
TagManager.Accounts.Containers.Versions.publish(version.path);
10698
const workspace = TagManager.Accounts.Containers.Workspaces.create(
@@ -120,16 +112,6 @@ function publishVersionAndQuickPreviewDraft(version) {
120112
// [END apps_script_tag_manager_publish_version]
121113

122114
// [START apps_script_tag_manager_create_user_environment]
123-
/**
124-
* Retrieves the container path from a container version path.
125-
* @param {string} versionPath The version path.
126-
* @return {string} The container path.
127-
*/
128-
function grabContainerPath(versionPath) {
129-
const pathParts = versionPath.split("/");
130-
return pathParts.slice(0, 4).join("/");
131-
}
132-
133115
/**
134116
* Creates and reauthorizes a user environment in a container that points
135117
* to a container version passed in as an argument.
@@ -138,7 +120,8 @@ function grabContainerPath(versionPath) {
138120
function createAndReauthorizeUserEnvironment(version) {
139121
try {
140122
// Creates a container version.
141-
const containerPath = grabContainerPath(version.path);
123+
const pathParts = version.path.split("/");
124+
const containerPath = pathParts.slice(0, 4).join("/");
142125
// Creates a user environment that points to a container version.
143126
const environment = TagManager.Accounts.Containers.Environments.create(
144127
{

advanced/youtube.gs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ function searchByKeyword() {
3030
console.log("Unable to search videos");
3131
return;
3232
}
33-
results.items.forEach((item) => {
33+
for (const item of results.items) {
3434
console.log("[%s] Title: %s", item.id.videoId, item.snippet.title);
35-
});
35+
}
3636
} catch (err) {
3737
// TODO (developer) - Handle exceptions from Youtube API
3838
console.log("Failed with an error %s", err.message);
@@ -179,7 +179,7 @@ function createSlides() {
179179
return;
180180
}
181181
// Add slides with videos and log the presentation URL to the user.
182-
youTubeVideos.forEach((video) => {
182+
for (const video of youTubeVideos) {
183183
const slide = presentation.appendSlide();
184184
slide.insertVideo(
185185
video.url,
@@ -188,7 +188,7 @@ function createSlides() {
188188
presentation.getPageWidth(),
189189
presentation.getPageHeight(),
190190
);
191-
});
191+
}
192192
console.log(presentation.getUrl());
193193
} catch (err) {
194194
// TODO (developer) - Handle exception

0 commit comments

Comments
 (0)