Skip to content

I've added a new JavaScript function called parseQueryString to `ex… #802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Mwessc
Copy link

@Mwessc Mwessc commented Jun 14, 2025

…amples/Apps_script_and_Workspace_codelab/parseQueryString.js`.

This function takes a URL query string and turns it into an object. The keys in this object are the query parameters, and their values are the corresponding values from the query string.

Here's what parseQueryString can do:

  • It works whether or not the query string starts with a '?'.
  • It correctly handles both single and multiple parameters.
  • If you have the same parameter multiple times, it will store all its values in an array.
  • It can deal with URL-encoded characters in both parameter names and values.
  • If you give it an empty, null, or undefined query string, it will return an empty object.
  • It can handle parameters that don't have values (like key=) and parameters without an equals sign (like key, which will result in key: '').

I've also included unit tests for this function in examples/Apps_script_and_Workspace_codelab/parseQueryString.test.js. These tests cover a wide variety of situations, including:

  • Empty and null/undefined inputs.
  • Simple and multiple parameters.
  • Parameters with multiple values.
  • URL-encoded characters.
  • Query strings with and without a leading '?'.
  • Parameters with no values or no equals sign.
  • More complex combinations of these scenarios.

You can run these tests in a basic JavaScript environment, like a browser console or Node.js, and they will log their results.

…amples/Apps_script_and_Workspace_codelab/parseQueryString.js`.

This function takes a URL query string and turns it into an object. The keys in this object are the query parameters, and their values are the corresponding values from the query string.

Here's what `parseQueryString` can do:
- It works whether or not the query string starts with a '?'.
- It correctly handles both single and multiple parameters.
- If you have the same parameter multiple times, it will store all its values in an array.
- It can deal with URL-encoded characters in both parameter names and values.
- If you give it an empty, null, or undefined query string, it will return an empty object.
- It can handle parameters that don't have values (like `key=`) and parameters without an equals sign (like `key`, which will result in `key: ''`).

I've also included unit tests for this function in `examples/Apps_script_and_Workspace_codelab/parseQueryString.test.js`. These tests cover a wide variety of situations, including:
- Empty and null/undefined inputs.
- Simple and multiple parameters.
- Parameters with multiple values.
- URL-encoded characters.
- Query strings with and without a leading '?'.
- Parameters with no values or no equals sign.
- More complex combinations of these scenarios.

You can run these tests in a basic JavaScript environment, like a browser console or Node.js, and they will log their results.
Copy link

google-cla bot commented Jun 14, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions bot added status:awaiting review PR awaiting review from a maintainer component:examples Issues/PR referencing examples folder labels Jun 14, 2025
@andycandy
Copy link
Collaborator

I'm a bit unsure about how this addition fits into the cookbook repo and what purpose it serves. Could you clarify that a bit?

@Giom-V Giom-V added the AI generated Looks like AI generated content label Jun 18, 2025
@Giom-V
Copy link
Collaborator

Giom-V commented Jun 18, 2025

@Mwessc While I can see that the function can be useful in some case, I fail to see what it brings to the appscript integration of Gemini. Can you clarify?

@Mwessc
Copy link
Author

Mwessc commented Jun 20, 2025

The parseQueryString function brings significant utility to the App Script integration of Gemini by simplifying how your scripts can interact with web-based APIs and data, which is often the case when dealing with services like Gemini. Here's a breakdown of its value:

Simplified Parameter Handling for Gemini Requests: When interacting with Gemini through web service calls (which is common in App Script), you often need to construct URLs with specific parameters to define your query. This function allows you to easily take a query string, potentially built dynamically within your App Script, and convert it into a structured JavaScript object. This makes it much easier to access and manage the individual parameters you need to send to Gemini.

Parsing Gemini API Response Data: While the direct response from a Gemini API might be in JSON or another structured format, sometimes APIs (including those Gemini might interact with internally or that your App Script might use in conjunction with Gemini) pass data back in the form of URLs with query parameters. This function provides a straightforward way to extract information encoded in this manner.

Flexibility in Handling User Input: If your App Script allows users to provide URLs related to their interaction with Gemini, this function can robustly parse those URLs, regardless of whether they start with a ?, have multiple parameters, or contain URL-encoded characters. This ensures your script can reliably extract the relevant information from user-provided input.

Robustness and Error Handling: The function is designed to handle various edge cases like empty strings, null or undefined input, and parameters without values. This makes your App Script more resilient to unexpected or poorly formatted data, leading to a more stable integration with Gemini.

In essence, parseQueryString acts as a handy utility to bridge the gap between URL-based data (common in web interactions) and the structured data formats that are easier to work with within your App Script logic when integrating with Gemini. It reduces the need for manual string manipulation and makes your code cleaner, more readable, and less prone to errors when dealing with URL parameters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AI generated Looks like AI generated content component:examples Issues/PR referencing examples folder status:awaiting review PR awaiting review from a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants