Skip to content

Commit 3d69059

Browse files
Apps script samples (#311)
* Update classroom.gs * Update region tags * Update target.js * Update adsense.gs Co-authored-by: Priyankarp24 <[email protected]>
1 parent 822bf78 commit 3d69059

File tree

4 files changed

+62
-47
lines changed

4 files changed

+62
-47
lines changed

advanced/adsense.gs

+33-33
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ function listAccounts() {
2121
let pageToken;
2222
do {
2323
const response = AdSense.Accounts.list({pageToken: pageToken});
24-
if (response.accounts) {
25-
for (const account of response.accounts) {
26-
Logger.log('Found account with resource name "%s" and display name "%s".',
27-
account.name, account.displayName);
28-
}
29-
} else {
24+
if (!response.accounts) {
3025
Logger.log('No accounts found.');
26+
return;
27+
}
28+
for (const account of response.accounts) {
29+
Logger.log('Found account with resource name "%s" and display name "%s".',
30+
account.name, account.displayName);
3131
}
3232
pageToken = response.nextPageToken;
3333
} while (pageToken);
@@ -47,15 +47,15 @@ function listAdClients(accountName) {
4747
const response = AdSense.Accounts.Adclients.list(accountName, {
4848
pageToken: pageToken
4949
});
50-
if (response.adClients) {
51-
for (const adClient of response.adClients) {
52-
Logger.log('Found ad client for product "%s" with resource name "%s".',
53-
adClient.productCode, adClient.name);
54-
Logger.log('Reporting dimension ID: %s',
55-
adClient.reportingDimensionId ?? 'None');
56-
}
57-
} else {
50+
if (!response.adClients) {
5851
Logger.log('No ad clients found for this account.');
52+
return;
53+
}
54+
for (const adClient of response.adClients) {
55+
Logger.log('Found ad client for product "%s" with resource name "%s".',
56+
adClient.productCode, adClient.name);
57+
Logger.log('Reporting dimension ID: %s',
58+
adClient.reportingDimensionId ?? 'None');
5959
}
6060
pageToken = response.nextPageToken;
6161
} while (pageToken);
@@ -75,13 +75,13 @@ function listAdUnits(adClientName) {
7575
pageSize: 50,
7676
pageToken: pageToken
7777
});
78-
if (response.adUnits) {
79-
for (const adUnit of response.adUnits) {
80-
Logger.log('Found ad unit with resource name "%s" and display name "%s".',
81-
adUnit.name, adUnit.displayName);
82-
}
83-
} else {
78+
if (!response.adUnits) {
8479
Logger.log('No ad units found for this ad client.');
80+
return;
81+
}
82+
for (const adUnit of response.adUnits) {
83+
Logger.log('Found ad unit with resource name "%s" and display name "%s".',
84+
adUnit.name, adUnit.displayName);
8585
}
8686

8787
pageToken = response.nextPageToken;
@@ -114,22 +114,22 @@ function generateReport(accountName, adClientReportingDimensionId) {
114114
orderBy: ['+DATE']
115115
});
116116

117-
if (report.rows) {
118-
const spreadsheet = SpreadsheetApp.create('AdSense Report');
119-
const sheet = spreadsheet.getActiveSheet();
117+
if (!report.rows) {
118+
Logger.log('No rows returned.');
119+
return;
120+
}
121+
const spreadsheet = SpreadsheetApp.create('AdSense Report');
122+
const sheet = spreadsheet.getActiveSheet();
120123

121-
// Append the headers.
122-
sheet.appendRow(report.headers.map((header) => header.name));
124+
// Append the headers.
125+
sheet.appendRow(report.headers.map((header) => header.name));
123126

124-
// Append the results.
125-
sheet.getRange(2, 1, report.rows.length, report.headers.length)
126-
.setValues(report.rows.map((row) => row.cells.map((cell) => cell.value)));
127+
// Append the results.
128+
sheet.getRange(2, 1, report.rows.length, report.headers.length)
129+
.setValues(report.rows.map((row) => row.cells.map((cell) => cell.value)));
127130

128-
Logger.log('Report spreadsheet created: %s',
129-
spreadsheet.getUrl());
130-
} else {
131-
Logger.log('No rows returned.');
132-
}
131+
Logger.log('Report spreadsheet created: %s',
132+
spreadsheet.getUrl());
133133
}
134134

135135
/**

advanced/classroom.gs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
// [START classroom_list_courses]
16+
// [START apps_script_classroom_list_courses]
1717
/**
1818
* Lists 10 course names and IDs.
1919
*/
@@ -41,4 +41,4 @@ function listCourses() {
4141
Logger.log('Failed with error %s', err.message);
4242
}
4343
}
44-
// [END classroom_list_courses]
44+
// [END apps_script_classroom_list_courses]

advanced/slides.gs

+12-12
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
// [START slides_create_presentation]
17+
// [START apps_script_slides_create_presentation]
1818
/**
1919
* Create a new presentation.
2020
* @return {string} presentation Id.
@@ -31,9 +31,9 @@ function createPresentation() {
3131
Logger.log('Failed with error %s', e.message);
3232
}
3333
}
34-
// [END slides_create_presentation]
34+
// [END apps_script_slides_create_presentation]
3535

36-
// [START slides_create_slide]
36+
// [START apps_script_slides_create_slide]
3737
/**
3838
* Create a new slide.
3939
* @param {string} presentationId The presentation to add the slide to.
@@ -63,9 +63,9 @@ function createSlide(presentationId) {
6363
Logger.log('Failed with error %s', e.message);
6464
}
6565
}
66-
// [END slides_create_slide]
66+
// [END apps_script_slides_create_slide]
6767

68-
// [START slides_read_page]
68+
// [START apps_script_slides_read_page]
6969
/**
7070
* Read page element IDs.
7171
* @param {string} presentationId The presentation to read from.
@@ -86,9 +86,9 @@ function readPageElementIds(presentationId, pageId) {
8686
Logger.log('Failed with error %s', e.message);
8787
}
8888
}
89-
// [END slides_read_page]
89+
// [END apps_script_slides_read_page]
9090

91-
// [START slides_add_text_box]
91+
// [START apps_script_slides_add_text_box]
9292
/**
9393
* Add a new text box with text to a page.
9494
* @param {string} presentationId The presentation ID.
@@ -144,9 +144,9 @@ function addTextBox(presentationId, pageId) {
144144
Logger.log('Failed with error %s', e.message);
145145
}
146146
}
147-
// [END slides_add_text_box]
147+
// [END apps_script_slides_add_text_box]
148148

149-
// [START slides_format_shape_text]
149+
// [START apps_script_slides_format_shape_text]
150150
/**
151151
* Format the text in a shape.
152152
* @param {string} presentationId The presentation ID.
@@ -188,9 +188,9 @@ function formatShapeText(presentationId, shapeId) {
188188
Logger.log('Failed with error %s', e.message);
189189
}
190190
}
191-
// [END slides_format_shape_text]
191+
// [END apps_script_slides_format_shape_text]
192192

193-
// [START slides_save_thumbnail]
193+
// [START apps_script_slides_save_thumbnail]
194194
/**
195195
* Saves a thumbnail image of the current Google Slide presentation in Google Drive.
196196
* Logs the image URL.
@@ -215,4 +215,4 @@ function saveThumbnailImage(i) {
215215
Logger.log('Failed with error %s', e.message);
216216
}
217217
}
218-
// [END slides_save_thumbnail]
218+
// [END apps_script_slides_save_thumbnail]

apps-script/execute/target.js

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
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+
*/
116
// [START apps_script_api_execute]
217
/**
318
* Return the set of folder names contained in the user's root folder as an

0 commit comments

Comments
 (0)