Skip to content

Commit 66b37d0

Browse files
committed
Import custom func demo from Next 24
1 parent b33ea37 commit 66b37d0

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

ai/custom-func-ai-studio/Code.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright 2024 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
18+
/**
19+
* Passes a prompt and a data range to Gemini AI.
20+
*
21+
* @param {range} range The range of cells.
22+
* @param {string} prompt The text prompt as a string or single cell reference.
23+
* @return The Gemini response.
24+
* @customfunction
25+
*/
26+
function gemini(range,prompt) {
27+
prompt = `For the range of cells ${range}, ${prompt}`
28+
return getAiSummary(prompt);
29+
}

ai/custom-func-ai-studio/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Google Sheets Custom Function with AI Studio
2+
3+
## Project Description
4+
5+
Google Sheets Custom Function to be used as a bound Apps Script project with a Google Sheets Spreadsheet
6+
7+
## Prerequisites
8+
9+
* Google Cloud Project (aka Standard Cloud Project for Apps Script) with billing enabled
10+
11+
## Set up your environment
12+
13+
1. Create a Cloud Project
14+
1. Enable Generative Language API - (may skip as is automatically done in step 2)
15+
1. Create a Google Gemini API Key
16+
1. Navigate to https://aistudio.google.com/app/apikey
17+
1. Create API key for existing project from step 1
18+
1. Copy the generated key for use in the next step.
19+
1. Open an Apps Script Project bound to a Google Sheets Spreadsheet
20+
1. From Project Settings, change project to GCP project number of Cloud Project from step 1
21+
1. Add a Script Property. Enter `api_key` as the property name and use the Gemini API Key as the value
22+
1. Add the project code to Apps Script
23+
24+
## Usage
25+
26+
Insert a custom function in Google Sheets, passing a range and a prompt as parameters
27+
28+
Example:
29+
30+
```
31+
=gemini(A1:A10,"Extract colors from the product description")
32+
```
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"timeZone": "America/Los_Angeles",
3+
"dependencies": {
4+
},
5+
"exceptionLogging": "STACKDRIVER",
6+
"runtimeVersion": "V8"
7+
}

ai/custom-func-ai-studio/gemini.js

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright 2024 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
/**
18+
* Packages prompt and necessary settings, then sends a request to the
19+
* Generative Language API. Returns the text string response, extracted from the
20+
* Gemini AI response object.
21+
*
22+
* @param {string} prompt String representing the prompt for Gemini AI call.
23+
* @return {string} Result of Gemini AI in string format.
24+
*/
25+
function getAiSummary(prompt) {
26+
const data = {
27+
"contents": [{
28+
"parts": [{
29+
"text": prompt
30+
}]
31+
}],
32+
"generationConfig": {
33+
"temperature": 0.2,
34+
"topK": 1,
35+
"topP": 1,
36+
"maxOutputTokens": 2048,
37+
"stopSequences": []
38+
},
39+
"safetySettings": [
40+
{
41+
"category": "HARM_CATEGORY_HARASSMENT",
42+
"threshold": "BLOCK_NONE"
43+
},
44+
{
45+
"category": "HARM_CATEGORY_HATE_SPEECH",
46+
"threshold": "BLOCK_NONE"
47+
},
48+
{
49+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
50+
"threshold": "BLOCK_NONE"
51+
},
52+
{
53+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
54+
"threshold": "BLOCK_NONE"
55+
}
56+
]
57+
};
58+
const options = {
59+
'method': 'post',
60+
'contentType': 'application/json',
61+
'payload': JSON.stringify(data) // Convert the JavaScript object to a JSON string.
62+
};
63+
64+
const apiKey = PropertiesService.getScriptProperties().getProperty('api_key');
65+
let response = UrlFetchApp.fetch('https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=' + apiKey, options);
66+
67+
const payload = JSON.parse(response.getContentText());
68+
const text = payload.candidates[0].content.parts[0].text;
69+
70+
return text;
71+
72+
}

0 commit comments

Comments
 (0)