The LM function unleashes the advanced capabilities of AI language models directly in Google Sheets. It converts natural language prompts into actionable outputsโgenerating content, analyzing data, and answering questionsโwithout leaving your spreadsheet.
=LM(prompt, [model], [use_cache], [output_mode], [max_tokens], [temperature], [image_url])
Parameter | Data Type | Required | Description |
---|---|---|---|
prompt |
|
Required | The instruction or question for the AI model. Supports cell references using curly braces (e.g.,
|
model |
|
Optional | The AI model to use in the format
|
use_cache |
|
Optional | Set to
|
output_mode |
|
Optional | Controls result display: leave blank to return text in a single cell; use
|
max_tokens |
|
Optional | The maximum number of tokens allowed in the generated response (e.g.,
|
temperature |
|
Optional | Adjusts the randomness of the output (from
|
image_url |
|
Optional | A URL pointing to an image to include with your prompt (applicable for models that support vision features). |
You can make prompts dynamic by referencing other cells. Examples include:
{A2}
{A}
{Customer Name}
{Sheet2!A1}
Example:
=LM("Summarize the following customer feedback: {Feedback}")
Use the output_mode parameter to format list outputs:
=LM("List 5 healthy breakfast ideas", , true, "list")
This returns each breakfast idea in its own cell vertically.
=LM("List 5 primary colors", , true, "hlist")
This returns each color in its own cell horizontally.
Enable caching to save API costs and improve speed:
=LM("Explain quantum computing in simple terms", "Gemini/gemini-1.5-pro", true)
Adjust the temperature parameter for deterministic or creative outputs:
=LM("Write a formal business email to request a meeting", , , , , 0.1)
Produces a focused, consistent response.
=LM("Write a short poem about autumn", , , , , 0.9)
Generates a more creative, varied poem.
For models with vision support, include an image URL:
=LM("Describe what's in this image and identify any issues", "Gemini/gemini-1.5-pro", , , , , "https://example.com/product_photo.jpg")
Retrieves a simple factual answer.
=LM("What is the capital of France?")
Expected output:
The capital of France is Paris.
Generates a product description for an eco-friendly water bottle.
=LM("Write a short product description for an eco-friendly water bottle made of recycled materials.")
Analyzes a specified data range using cell references.
=LM("Based on the data in {A2:B10}, what trends do you observe?")
Creates a vertical list of ideas.
=LM("List 5 ways to improve team communication", , true, "list")
Translates text from English to Spanish using a specific model.
=LM("Translate the following English text to Spanish: {C5}", "Gemini/gemini-1.5-pro")
Generates a personalized email based on row data.
=LM("Write a thank you email to {Customer Name} for their purchase of {Product}. Mention that their order #{Order ID} will arrive by {Delivery Date}.")
Produces a creative short story using a higher temperature.
=LM("Write a short story about a robot learning to paint", , , , 800, 0.9)
Analyzes a product image for potential issues.
=LM("What's wrong with this product image?", "Gemini/gemini-1.5-pro", , , , , {Image URL})
Ensure the provider name is entered correctly in the model parameter and that the corresponding API key is configured.
Verify that you have set the API key in the Sheet Workflows settings; otherwise, the function cannot make API calls.
Adjust the temperature parameter to obtain more consistent outputs, and reword your prompt for clarity.
Enable caching by setting use_cache to
true
Confirm that cell references use the correct format, such as
{A1}
{Column Name}
Check that you have used the proper output_mode parameter (
"list"
"hlist"
If you receive an error like "Failed to get response from [Provider]," check your internet connection, validate your API key, and ensure the provider is operational.
Make sure the model supports vision capabilities, verify the image URL is correct and accessible, and ensure the image format is supported.
It depends on your task. Models like Gemini 1.5 Pro and Claude are versatile for complex reasoning, while others might specialize in speed or specific tasks. Experiment to determine which fits your needs best.
Costs vary by provider and model configuration. Many providers offer free tiers with usage limits, so check the pricing details of your chosen provider.
The
"list"
"hlist"
Use a lower temperature value (such as
0.1
0.3
Yes! You can nest the output of LM with other functions, allowing powerful integrations of AI-generated content and traditional spreadsheet operations.
The limit depends on the AI model in use, but most models can handle several thousand tokens. For very large inputs, consider splitting the text.
Yes. Each provider requires its own API key configuration, which must be set in the Sheet Workflows settings.
When use_cache is set to
true
The LM function is primarily designed for cell use within Google Sheets. For more advanced, programmatic applications, consider using the providerโs API directly in your scripts.
Upload your image to a web-accessible location (such as Google Drive with a shareable link) and use that URL in the image_url parameter.
5 months ago
1 month ago
1 month ago
5 months ago