@@ -15,54 +15,93 @@ limitations under the License.
15
15
*/
16
16
17
17
/**
18
- * Builds the card to display in the side panel of gmail.
19
- * @return {CardService.Card } The card to show to the user.
18
+ * Builds the main card displayed on the Gmail homepage.
19
+ *
20
+ * @returns {Card } - The homepage card.
20
21
*/
21
-
22
22
function buildHomepageCard ( ) {
23
- const imageUrl = 'https://fonts.gstatic.com/s/i/googlematerialicons/dynamic_feed/v6/black-24dp/1x/gm_dynamic_feed_black_24dp.png' ;
24
-
25
- const cardHeader = CardService . newCardHeader ( )
26
- . setImageUrl ( imageUrl )
27
- . setImageStyle ( CardService . ImageStyle . CIRCLE )
28
- . setTitle ( "Analyze your Gmail" ) ;
29
-
30
- const analyzeSentimentAction = CardService . newAction ( )
31
- . setFunctionName ( 'analyzeSentiment' ) ;
32
- const analyzeSentimentBtn = CardService . newTextButton ( )
33
- . setText ( 'Analyze emails' )
34
- . setOnClickAction ( analyzeSentimentAction )
35
- . setTextButtonStyle ( CardService . TextButtonStyle . FILLED )
36
- . setBackgroundColor ( '#FF0000' ) ;
37
-
38
- const generateSampleEmailAction = CardService . newAction ( )
39
- . setFunctionName ( 'generateSampleEmails' ) ;
40
-
41
- const generateSampleEmailsBtn = CardService . newTextButton ( )
42
- . setText ( 'Generate sample emails' )
43
- . setOnClickAction ( generateSampleEmailAction )
44
- . setTextButtonStyle ( CardService . TextButtonStyle . FILLED )
45
- . setBackgroundColor ( '#34A853' ) ;
46
-
47
- const buttonSet = CardService . newButtonSet ( )
48
- . addButton ( generateSampleEmailsBtn )
49
- . addButton ( analyzeSentimentBtn ) ;
50
-
51
- const section = CardService . newCardSection ( )
52
- . addWidget ( buttonSet ) ;
53
-
54
- const card = CardService . newCardBuilder ( )
55
- . setHeader ( cardHeader )
56
- . addSection ( section ) ;
57
-
58
- return card . build ( ) ;
23
+ // Create a new card builder
24
+ const cardBuilder = CardService . newCardBuilder ( ) ;
25
+
26
+ // Create a card header
27
+ const cardHeader = CardService . newCardHeader ( ) ;
28
+ cardHeader . setImageUrl ( 'https://fonts.gstatic.com/s/i/googlematerialicons/mail/v6/black-24dp/1x/gm_mail_black_24dp.png' ) ;
29
+ cardHeader . setImageStyle ( CardService . ImageStyle . CIRCLE ) ;
30
+ cardHeader . setTitle ( "Analyze your Gmail" ) ;
31
+
32
+ // Add the header to the card
33
+ cardBuilder . setHeader ( cardHeader ) ;
34
+
35
+ // Create a card section
36
+ const cardSection = CardService . newCardSection ( ) ;
37
+
38
+ // Create buttons for generating sample emails and analyzing sentiment
39
+ const buttonSet = CardService . newButtonSet ( ) ;
40
+
41
+ // Create "Generate sample emails" button
42
+ const generateButton = createFilledButton ( 'Generate sample emails' , 'generateSampleEmails' , '#34A853' ) ;
43
+ buttonSet . addButton ( generateButton ) ;
44
+
45
+ // Create "Analyze emails" button
46
+ const analyzeButton = createFilledButton ( 'Analyze emails' , 'analyzeSentiment' , '#FF0000' ) ;
47
+ buttonSet . addButton ( analyzeButton ) ;
48
+
49
+ // Add the button set to the section
50
+ cardSection . addWidget ( buttonSet ) ;
51
+
52
+ // Add the section to the card
53
+ cardBuilder . addSection ( cardSection ) ;
54
+
55
+ // Build and return the card
56
+ return cardBuilder . build ( ) ;
59
57
}
60
58
61
- function buildNotificationResponse ( notificationText ) {
62
- const notification = CardService . newNotification ( ) . setText ( notificationText ) ;
59
+ /**
60
+ * Creates a filled text button with the specified text, function, and color.
61
+ *
62
+ * @param {string } text - The text to display on the button.
63
+ * @param {string } functionName - The name of the function to call when the button is clicked.
64
+ * @param {string } color - The background color of the button.
65
+ * @returns {TextButton } - The created text button.
66
+ */
67
+ function createFilledButton ( text , functionName , color ) {
68
+ // Create a new text button
69
+ const textButton = CardService . newTextButton ( ) ;
70
+
71
+ // Set the button text
72
+ textButton . setText ( text ) ;
73
+
74
+ // Set the action to perform when the button is clicked
75
+ const action = CardService . newAction ( ) ;
76
+ action . setFunctionName ( functionName ) ;
77
+ textButton . setOnClickAction ( action ) ;
63
78
64
- const actionResponse = CardService . newActionResponseBuilder ( )
65
- . setNotification ( notification ) ;
79
+ // Set the button style to filled
80
+ textButton . setTextButtonStyle ( CardService . TextButtonStyle . FILLED ) ;
66
81
67
- return actionResponse . build ( ) ;
82
+ // Set the background color
83
+ textButton . setBackgroundColor ( color ) ;
84
+
85
+ return textButton ;
68
86
}
87
+
88
+ /**
89
+ * Creates a notification response with the specified text.
90
+ *
91
+ * @param {string } notificationText - The text to display in the notification.
92
+ * @returns {ActionResponse } - The created action response.
93
+ */
94
+ function buildNotificationResponse ( notificationText ) {
95
+ // Create a new notification
96
+ const notification = CardService . newNotification ( ) ;
97
+ notification . setText ( notificationText ) ;
98
+
99
+ // Create a new action response builder
100
+ const actionResponseBuilder = CardService . newActionResponseBuilder ( ) ;
101
+
102
+ // Set the notification for the action response
103
+ actionResponseBuilder . setNotification ( notification ) ;
104
+
105
+ // Build and return the action response
106
+ return actionResponseBuilder . build ( ) ;
107
+ }
0 commit comments