-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Import custom func demo from Next 24
- Loading branch information
Showing
4 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
Copyright 2024 Google LLC | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
https://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
|
||
/** | ||
* Passes a prompt and a data range to Gemini AI. | ||
* | ||
* @param {range} range The range of cells. | ||
* @param {string} prompt The text prompt as a string or single cell reference. | ||
* @return The Gemini response. | ||
* @customfunction | ||
*/ | ||
function gemini(range,prompt) { | ||
prompt = `For the range of cells ${range}, ${prompt}` | ||
return getAiSummary(prompt); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Google Sheets Custom Function with AI Studio | ||
|
||
## Project Description | ||
|
||
Google Sheets Custom Function to be used as a bound Apps Script project with a Google Sheets Spreadsheet | ||
|
||
## Prerequisites | ||
|
||
* Google Cloud Project (aka Standard Cloud Project for Apps Script) with billing enabled | ||
|
||
## Set up your environment | ||
|
||
1. Create a Cloud Project | ||
1. Enable Generative Language API - (may skip as is automatically done in step 2) | ||
1. Create a Google Gemini API Key | ||
1. Navigate to https://aistudio.google.com/app/apikey | ||
1. Create API key for existing project from step 1 | ||
1. Copy the generated key for use in the next step. | ||
1. Open an Apps Script Project bound to a Google Sheets Spreadsheet | ||
1. From Project Settings, change project to GCP project number of Cloud Project from step 1 | ||
1. Add a Script Property. Enter `api_key` as the property name and use the Gemini API Key as the value | ||
1. Add the project code to Apps Script | ||
|
||
## Usage | ||
|
||
Insert a custom function in Google Sheets, passing a range and a prompt as parameters | ||
|
||
Example: | ||
|
||
``` | ||
=gemini(A1:A10,"Extract colors from the product description") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"timeZone": "America/Los_Angeles", | ||
"dependencies": { | ||
}, | ||
"exceptionLogging": "STACKDRIVER", | ||
"runtimeVersion": "V8" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
Copyright 2024 Google LLC | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
https://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Packages prompt and necessary settings, then sends a request to the | ||
* Generative Language API. Returns the text string response, extracted from the | ||
* Gemini AI response object. | ||
* | ||
* @param {string} prompt String representing the prompt for Gemini AI call. | ||
* @return {string} Result of Gemini AI in string format. | ||
*/ | ||
function getAiSummary(prompt) { | ||
const data = { | ||
"contents": [{ | ||
"parts": [{ | ||
"text": prompt | ||
}] | ||
}], | ||
"generationConfig": { | ||
"temperature": 0.2, | ||
"topK": 1, | ||
"topP": 1, | ||
"maxOutputTokens": 2048, | ||
"stopSequences": [] | ||
}, | ||
"safetySettings": [ | ||
{ | ||
"category": "HARM_CATEGORY_HARASSMENT", | ||
"threshold": "BLOCK_NONE" | ||
}, | ||
{ | ||
"category": "HARM_CATEGORY_HATE_SPEECH", | ||
"threshold": "BLOCK_NONE" | ||
}, | ||
{ | ||
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", | ||
"threshold": "BLOCK_NONE" | ||
}, | ||
{ | ||
"category": "HARM_CATEGORY_DANGEROUS_CONTENT", | ||
"threshold": "BLOCK_NONE" | ||
} | ||
] | ||
}; | ||
const options = { | ||
'method': 'post', | ||
'contentType': 'application/json', | ||
'payload': JSON.stringify(data) // Convert the JavaScript object to a JSON string. | ||
}; | ||
|
||
const apiKey = PropertiesService.getScriptProperties().getProperty('api_key'); | ||
let response = UrlFetchApp.fetch('https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=' + apiKey, options); | ||
|
||
const payload = JSON.parse(response.getContentText()); | ||
const text = payload.candidates[0].content.parts[0].text; | ||
|
||
return text; | ||
|
||
} |