ChatGPT Example: Crafting More Advanced Code

May 29, 2023 | 7 minutes read

Here’s an example implementation of of the ideas from ChatGPT: Crafting More Advanced Code showing prompts and some responses. These prompts are intentionally structured so you can use them in a new chat for each prompt to try to avoid contextual history loss during your conversation.

Note that the results are untested, this is just an example.

Here’s how you could phrase each prompt with enough context for a new chat:

1. Creating an Outline

Start by providing context for the desired script:

I am designing a Google Apps script with several functions: creating a button in Gmail, triggering a search operation when this button is pressed, and identifying emails within the search results that haven't received a response to my latest email. Could you help me outline this script without writing code?

ChatGPT Response

Certainly! I can help you outline the steps for designing a Google Apps script with the functionalities you mentioned. Here’s a high-level outline of the script:

  1. Create a button in Gmail: To create a button in Gmail, you’ll need to use the Gmail Add-ons feature provided by Google Workspace. This allows you to extend the functionality of Gmail. Within the add-on, you can define a button that will appear in the Gmail user interface.

  2. Define button functionality: When the button is pressed, it should trigger a specific action. In this case, you want to perform a search operation on the emails and identify those that haven’t received a response. You can define a function that will be called when the button is pressed.

  3. Perform search operation: Within the function triggered by the button press, you’ll need to use the Gmail service provided by Google Apps Script to perform a search operation. You can use the method to search for specific emails based on various criteria, such as the sender, subject, or date.

  4. Identify emails without response: Once you have the search results, you’ll need to iterate through each email and determine whether it has received a response. One approach is to compare the latest email you sent with the received emails in the search results. If an email in the search results doesn’t have a response from you, it can be considered as not having received a response.

  5. Take action on identified emails: After identifying the emails without a response, you can take any desired action. For example, you could mark them with a specific label, apply a certain formatting, or display a notification to alert you about the unanswered emails.

  6. Additional features: Depending on your requirements, you can add additional features to the script. For example, you might want to schedule the search operation to run automatically at specific intervals or add a notification system to alert you when new unanswered emails are found.

Remember that this outline provides a general structure for your script. You will need to write the actual code to implement each step. The Google Apps Script documentation and examples can be a helpful resource as you write the code.

2. Defining Functionality for Each Step

Once you have the outline, you can define the functionalities for each step:

Button Creation:

Consider a Google Apps script designed to add a custom button to the Gmail interface. Could you describe the functionality this part of the script would need, without going into the specifics of the code?

ChatGPT Response

Certainly! To add a custom button to the Gmail interface using a Google Apps Script, here’s a description of the functionality this part of the script would typically need:

  1. Button Appearance: The script should define the visual appearance of the custom button. This includes specifying the button’s icon or image, label, size, color, and any additional styling.

  2. Button Placement: The script needs to specify where the custom button should be located within the Gmail interface. This could be in the toolbar, alongside other buttons, or in a specific section.

  3. Button Interaction: The script should handle user interactions with the custom button. This involves defining the actions or behavior that occur when the button is clicked. For example, it may trigger a specific function, open a sidebar or dialog, initiate an API call, or perform a series of actions.

  4. Authorization and Permissions: If the custom button requires access to certain Gmail data or functionality, the script should handle the necessary authorization and permissions. This ensures that the script has the required permissions to perform the intended actions.

  5. Dynamic Content: The script may need to fetch or display dynamic content based on the current state of the user’s inbox or selected email. For instance, it could retrieve specific information from the email, interact with external APIs, or perform calculations based on the email’s content.

  6. Error Handling: The script should include error handling mechanisms to gracefully handle and communicate any errors that may occur during the execution of the button’s functionality. This helps provide a smooth user experience and allows for troubleshooting.

  7. User Feedback: It can be beneficial to provide visual feedback to the user when they interact with the custom button. This could involve displaying notifications, progress indicators, success messages, or error messages to keep the user informed about the actions being performed.

These functionalities collectively enable the custom button to extend the Gmail interface’s capabilities and provide users with additional functionality or automation within their email workflows.

Search Functionality:

I'm working on a feature of a Google Apps script, which is initiated by a button press in Gmail. Once activated, it triggers a search operation within Gmail. Can you explain the functionality this feature should have, excluding the coding specifics?

ChatGPT Response…

Email Identification:

In the context of a Google Apps script, I'm designing a feature to identify unreplied emails within Gmail search results. Could you define the functionality this feature should possess, without detailing the coding specifics?

ChatGPT Response…

3. Writing the Code

With each step defined, you can now ask for code:

Button Creation Code:

Given a Google Apps script, I'm looking to write the section that creates a custom button in the Gmail interface. Can you assist with the code for this part?

ChatGPT Response

Certainly! I can assist you with creating a custom button in the Gmail interface using Google Apps Script. To add a custom button to the Gmail interface, you’ll need to create a Gmail Add-on.

Here’s an example code snippet that demonstrates how to create a custom button in the Gmail interface using Google Apps Script:

function createCustomButton() {
  var accessToken = ScriptApp.getOAuthToken();
  var button = CardService.newTextButton()
    .setText('Custom Button')

  var section = CardService.newCardSection()

  var card = CardService.newCardBuilder()

  var navItem = CardService.newNavigation().pushCard(card);
  var action = CardService.newUniversalAction()

  var addButtonToGmail = CardService.newActionResponseBuilder()
      .setText('Custom button clicked.'))

  return addButtonToGmail;

function handleButtonClick(e) {
  // This function will be executed when the custom button is clicked
  // Add your desired functionality here
  Logger.log('Custom button clicked!');

To use this code:

  1. Open the Google Apps Script editor by going to and creating a new project.
  2. Copy and paste the code into the script editor.
  3. Save the script.
  4. Deploy the script as a Gmail Add-on by clicking on “Deploy” > “Deploy as add-on” in the menu.
  5. Follow the prompts to configure the add-on settings and permissions.
  6. Once deployed, you should see the custom button in the Gmail interface.

The createCustomButton function creates the custom button and specifies its behavior. The handleButtonClick function is the callback function that will be executed when the custom button is clicked. You can add your desired functionality inside the handleButtonClick function.

Remember to customize the code according to your requirements, such as changing the button label, setting the URL to open when the button is clicked, and modifying the handleButtonClick function to perform your desired actions.

Please note that deploying a Gmail Add-on requires certain permissions and may undergo a review process by Google before it becomes available to users in the Gmail interface.

Let me know if you need any further assistance!

Search Functionality Code:

In a Google Apps script, I need to code a feature that initiates a Gmail search operation upon a button press. Could you help me write the code for this functionality?

ChatGPT Response…

Email Identification Code:

I'm writing a Google Apps script where a feature identifies unreplied emails within the Gmail search results. Can you assist me in writing the code for this?

ChatGPT Response…

That wraps up our example. We’ve seen how we can use ChatGPT to assist us with coding tasks, breaking complex problems into manageable parts. Remember, the provided code is a guideline, so feel free to modify it to fit your unique requirements.

This technique isn’t just for Google Apps Scripts. You can apply the same strategy to break down complex tasks in any coding project. Keep iterating, keep refining, and most importantly, don’t forget to have fun along the way. Happy coding!