Skip to content

Commit 003da5d

Browse files
fix(mashups): resolve type errors (#589)
* fix(mashups): resolve type errors Fixes TypeScript check errors in the 'mashups' directory. - Adds JSDoc annotations to resolve implicit 'any' types. - Corrects argument types for API calls. - Defines a local type for the onEdit event object. - Refactors code to avoid variable reassignment with different types. * feat(mashups): resolve type errors and use let/const This commit addresses two main points: 1. Resolves all TypeScript check errors in the 'mashups' directory by: - Adding JSDoc annotations to resolve implicit 'any' types. - Correcting argument types for API calls. - Defining a local type for the onEdit event object. - Refactoring code to avoid variable reassignment with different types. 2. Refactors all `var` declarations to use `let` and `const` for improved scope management and code readability, following modern JavaScript standards. --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 275a497 commit 003da5d

File tree

10 files changed

+88
-74
lines changed

10 files changed

+88
-74
lines changed

mashups/sheets2calendar.gs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,38 @@
66
*/
77
function createEventsFromSpreadsheet() {
88
// Open the spreadsheet and get the data.
9-
var ss = SpreadsheetApp.openByUrl('ENTER SPREADSHEET URL HERE');
10-
var sheet = ss.getSheets()[0];
11-
var data = sheet.getDataRange().getValues();
9+
const ss = SpreadsheetApp.openByUrl('ENTER SPREADSHEET URL HERE');
10+
const sheet = ss.getSheets()[0];
11+
/** @type {string[][]} */
12+
const data = sheet.getDataRange().getValues();
1213

1314
// Remove any frozen rows from the data, since they contain headers.
1415
data.splice(sheet.getFrozenRows());
1516

1617
// Create an event for each row.
1718
data.forEach(function(row) {
18-
var title = row[0];
19-
var description = row[1];
20-
var emails = row[2];
19+
const title = row[0];
20+
const description = row[1];
21+
const emailsStr = row[2];
2122

2223
// Split the emails into an array and remove extra whitespace.
23-
emails = emails.split(',').map(function(email) {
24+
const emails = emailsStr.split(',').map(function(email) {
2425
return email.trim();
2526
});
2627

27-
var now = new Date();
28+
const now = new Date();
2829
// Start the event at the next hour mark.
29-
var start = new Date(now);
30+
const start = new Date(now);
3031
start.setHours(start.getHours() + 1);
3132
start.setMinutes(0);
3233
start.setSeconds(0);
3334
start.setMilliseconds(0);
3435
// End the event after 30 minutes.
35-
var end = new Date(start);
36+
const end = new Date(start);
3637
end.setMinutes(end.getMinutes() + 30);
3738

3839
// Create the calendar event and invite the guests.
39-
var event = CalendarApp.createEvent(title, start, end)
40+
const event = CalendarApp.createEvent(title, start, end)
4041
.setDescription(description);
4142
emails.forEach(function(email) {
4243
event.addGuest(email);

mashups/sheets2chat.gs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/**
2+
* @typedef {Object} SheetEditEvent
3+
* @property {string} oldValue The old value of the cell.
4+
* @property {string} value The new value of the cell.
5+
*/
6+
17
/**
28
* Posts a message to a Hangouts Chat room every time the spreadsheet is edited.
39
* This script must be attached to the spreadsheet (created in Google Sheets under
@@ -8,16 +14,16 @@
814
* "From spreadsheet", "On edit".
915
* - Click "Save".
1016
*
11-
* @param {Object} e The onEdit event object.
17+
* @param {SheetEditEvent} e The onEdit event object.
1218
*/
1319
function sendChatMessageOnEdit(e) {
14-
var range = SpreadsheetApp.getActiveRange();
15-
var value = range.getValue();
16-
var oldValue = e.oldValue;
17-
var ss = range.getSheet().getParent();
20+
const range = SpreadsheetApp.getActiveRange();
21+
const value = range.getValue();
22+
const oldValue = e.oldValue;
23+
const ss = range.getSheet().getParent();
1824

1925
// Construct the message to send, based on the old and new value of the cell.
20-
var changeMessage;
26+
let changeMessage;
2127
if (oldValue && value) {
2228
changeMessage = Utilities.formatString('changed from "%s" to "%s"',
2329
oldValue, value);
@@ -26,17 +32,17 @@ function sendChatMessageOnEdit(e) {
2632
} else {
2733
changeMessage = 'cleared';
2834
}
29-
var message = Utilities.formatString(
35+
const message = Utilities.formatString(
3036
'The range %s was %s. <%s|Open spreadsheet>.',
3137
range.getA1Notation(), changeMessage, ss.getUrl());
3238

3339
// Follow these steps to create an incomming webhook URL for your chat room:
3440
// https://developers.google.com/hangouts/chat/how-tos/webhooks#define_an_incoming_webhook
35-
var webhookUrl = 'ENTER INCOMMING WEBHOOK URL HERE';
41+
const webhookUrl = 'ENTER INCOMMING WEBHOOK URL HERE';
3642

3743
// Use the spreadsheet's ID as a thread key, so that all messages go into the
3844
// same thread.
39-
var url = webhookUrl + '&threadKey=' + ss.getId();
45+
const url = webhookUrl + '&threadKey=' + ss.getId();
4046

4147
// Send the message.
4248
UrlFetchApp.fetch(url, {

mashups/sheets2contacts.gs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
*/
66
function createContactsFromSpreadsheet() {
77
// Open the spreadsheet and get the data.
8-
var ss = SpreadsheetApp.openByUrl('ENTER SPREADSHEET URL HERE');
9-
var sheet = ss.getSheets()[0];
10-
var data = sheet.getDataRange().getValues();
8+
const ss = SpreadsheetApp.openByUrl('ENTER SPREADSHEET URL HERE');
9+
const sheet = ss.getSheets()[0];
10+
const data = sheet.getDataRange().getValues();
1111

1212
// Remove any frozen rows from the data, since they contain headers.
1313
data.splice(sheet.getFrozenRows());
1414

1515
// Send a contact for each row.
1616
data.forEach(function(row) {
17-
var firstName = row[0];
18-
var lastName = row[1];
19-
var email = row[2];
17+
const firstName = row[0];
18+
const lastName = row[1];
19+
const email = row[2];
2020
ContactsApp.createContact(firstName, lastName, email);
2121
});
2222
}

mashups/sheets2docs.gs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,27 @@
66
*/
77
function createDocsFromSpreadsheet() {
88
// Open the spreadsheet and get the data.
9-
var ss = SpreadsheetApp.openByUrl('ENTER SPREADSHEET URL HERE');
10-
var sheet = ss.getSheets()[0];
11-
var data = sheet.getDataRange().getValues();
9+
const ss = SpreadsheetApp.openByUrl('ENTER SPREADSHEET URL HERE');
10+
const sheet = ss.getSheets()[0];
11+
/** @type {string[][]} */
12+
const data = sheet.getDataRange().getValues();
1213

1314
// Remove any frozen rows from the data, since they contain headers.
1415
data.splice(sheet.getFrozenRows());
1516

1617
// Create a document for each row.
1718
data.forEach(function(row) {
18-
var title = row[0];
19-
var content = row[1];
20-
var emails = row[2];
19+
const title = row[0];
20+
const content = row[1];
21+
const emailsStr = row[2];
2122

2223
// Split the emails into an array and remove extra whitespace.
23-
emails = emails.split(',').map(function(email) {
24+
const emails = emailsStr.split(',').map(function(email) {
2425
return email.trim();
2526
});
2627

2728
// Create the document, append the content, and share it out.
28-
var doc = DocumentApp.create(title);
29+
const doc = DocumentApp.create(title);
2930
doc.getBody().appendParagraph(content);
3031
doc.addEditors(emails);
3132
});

mashups/sheets2drive.gs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,31 @@
66
*/
77
function createDriveFilesFromSpreadsheet() {
88
// Open the spreadsheet and get the data.
9-
var ss = SpreadsheetApp.openByUrl('ENTER SPREADSHEET URL HERE');
10-
var sheet = ss.getSheets()[0];
11-
var data = sheet.getDataRange().getValues();
9+
const ss = SpreadsheetApp.openByUrl('ENTER SPREADSHEET URL HERE');
10+
const sheet = ss.getSheets()[0];
11+
/** @type {string[][]} */
12+
const data = sheet.getDataRange().getValues();
1213

1314
// Remove any frozen rows from the data, since they contain headers.
1415
data.splice(sheet.getFrozenRows());
1516

1617
// Create a PDF in Google Drive for each row.
1718
data.forEach(function(row) {
18-
var fileName = row[0];
19-
var htmlContent = row[1];
20-
var emails = row[2];
19+
const fileName = row[0];
20+
const htmlContent = row[1];
21+
const emailsStr = row[2];
2122

2223
// Split the emails into an array and remove extra whitespace.
23-
emails = emails.split(',').map(function(email) {
24+
const emails = emailsStr.split(',').map(function(email) {
2425
return email.trim();
2526
});
2627

2728
// Convert the HTML content to PDF.
28-
var html = Utilities.newBlob(htmlContent, 'text/html');
29-
var pdf = html.getAs('application/pdf');
29+
const html = Utilities.newBlob(htmlContent, 'text/html');
30+
const pdf = html.getAs('application/pdf');
3031

3132
// Create the Drive file and share it out.
32-
var file = DriveApp.createFile(pdf).setName(fileName);
33+
const file = DriveApp.createFile(pdf).setName(fileName);
3334
file.addEditors(emails);
3435
});
3536
}

mashups/sheets2forms.gs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,27 @@
66
*/
77
function createFormsFromSpreadsheet() {
88
// Open the spreadsheet and get the data.
9-
var ss = SpreadsheetApp.openByUrl('ENTER SPREADSHEET URL HERE');
10-
var sheet = ss.getSheets()[0];
11-
var data = sheet.getDataRange().getValues();
9+
const ss = SpreadsheetApp.openByUrl('ENTER SPREADSHEET URL HERE');
10+
const sheet = ss.getSheets()[0];
11+
/** @type {string[][]} */
12+
const data = sheet.getDataRange().getValues();
1213

1314
// Remove any frozen rows from the data, since they contain headers.
1415
data.splice(sheet.getFrozenRows());
1516

1617
// Create a form for each row.
1718
data.forEach(function(row) {
18-
var title = row[0];
19-
var question = row[1];
20-
var emails = row[2];
19+
const title = row[0];
20+
const question = row[1];
21+
const emailsStr = row[2];
2122

2223
// Split the emails into an array and remove extra whitespace.
23-
emails = emails.split(',').map(function(email) {
24+
const emails = emailsStr.split(',').map(function(email) {
2425
return email.trim();
2526
});
2627

2728
// Create the form, append the question, and share it out.
28-
var form = FormApp.create(title);
29+
const form = FormApp.create(title);
2930
form.addTextItem().setTitle(question);
3031
form.addEditors(emails);
3132
});

mashups/sheets2gmail.gs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@
66
*/
77
function sendEmailsFromSpreadsheet() {
88
// Open the spreadsheet and get the data.
9-
var ss = SpreadsheetApp.openByUrl('ENTER SPREADSHEET URL HERE');
10-
var sheet = ss.getSheets()[0];
11-
var data = sheet.getDataRange().getValues();
9+
const ss = SpreadsheetApp.openByUrl('ENTER SPREADSHEET URL HERE');
10+
const sheet = ss.getSheets()[0];
11+
/** @type {string[][]} */
12+
const data = sheet.getDataRange().getValues();
1213

1314
// Remove any frozen rows from the data, since they contain headers.
1415
data.splice(sheet.getFrozenRows());
1516

1617
// Send an email for each row.
1718
data.forEach(function(row) {
18-
var subject = row[0];
19-
var htmlMessage = row[1];
20-
var emails = row[2];
19+
const subject = row[0];
20+
const htmlMessage = row[1];
21+
const emails = row[2];
2122

2223
// Send the email.
23-
GmailApp.sendEmail(emails, subject, null, {
24+
GmailApp.sendEmail(emails, subject, '', {
2425
htmlBody: htmlMessage
2526
});
2627
});

mashups/sheets2maps.gs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
* @customFunction
1313
*/
1414
function COUNTY(address) {
15-
var results = Maps.newGeocoder().geocode(address).results;
15+
const results = Maps.newGeocoder().geocode(address).results;
1616
if (!results || results.length === 0) {
1717
throw new Error('Unknown address');
1818
}
19-
var counties = results[0].address_components.filter(function(component) {
19+
/** @type {{long_name: string, types: string[]}[]} */
20+
const addressComponents = results[0].address_components;
21+
const counties = addressComponents.filter(function(component) {
2022
return component.types.indexOf('administrative_area_level_2') >= 0;
2123
});
2224
if (!counties.length) {

mashups/sheets2slides.gs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,30 @@
66
*/
77
function createPresentationsFromSpreadsheet() {
88
// Open the spreadsheet and get the data.
9-
var ss = SpreadsheetApp.openByUrl('ENTER SPREADSHEET URL HERE');
10-
var sheet = ss.getSheets()[0];
11-
var data = sheet.getDataRange().getValues();
9+
const ss = SpreadsheetApp.openByUrl('ENTER SPREADSHEET URL HERE');
10+
const sheet = ss.getSheets()[0];
11+
/** @type {string[][]} */
12+
const data = sheet.getDataRange().getValues();
1213

1314
// Remove any frozen rows from the data, since they contain headers.
1415
data.splice(sheet.getFrozenRows());
1516

1617
// Create a presentation for each row.
1718
data.forEach(function(row) {
18-
var title = row[0];
19-
var content = row[1];
20-
var emails = row[2];
19+
const title = row[0];
20+
const content = row[1];
21+
const emailsStr = row[2];
2122

2223
// Split the emails into an array and remove extra whitespace.
23-
emails = emails.split(',').map(function(email) {
24+
const emails = emailsStr.split(',').map(function(email) {
2425
return email.trim();
2526
});
2627

2728
// Create the presentation, insert a new slide at the start, append the content,
2829
// and share it out.
29-
var presentation = SlidesApp.create(title);
30-
var slide = presentation.insertSlide(0, SlidesApp.PredefinedLayout.MAIN_POINT);
31-
var textBox = slide.getShapes()[0];
30+
const presentation = SlidesApp.create(title);
31+
const slide = presentation.insertSlide(0, SlidesApp.PredefinedLayout.MAIN_POINT);
32+
const textBox = slide.getShapes()[0];
3233
textBox.getText().appendParagraph(content);
3334
presentation.addEditors(emails);
3435
});

mashups/sheets2translate.gs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
* under "Tools > Script editor").
1010
*/
1111
function onEdit() {
12-
var range = SpreadsheetApp.getActiveRange();
13-
var value = range.getValue();
12+
const range = SpreadsheetApp.getActiveRange();
13+
const value = range.getValue();
1414
if (typeof value === 'string') {
15-
var translated = LanguageApp.translate(value, null, 'en');
15+
const translated = LanguageApp.translate(value, '', 'en');
1616
range.setNote(translated);
1717
}
1818
}

0 commit comments

Comments
 (0)