TASK_LM

Function Overview

The TASK_LM function runs language model operations in a cell in Google Sheets. It allows you to send prompts to various AI models, process the responses, and output the results directly to cells, as dropdown lists, or as Google Docs or text files.

Uses

  1. Generate text content in cells using AI models like Claude, GPT-4, or Gemini
  2. Create lists of options in vertical columns, horizontal rows, or as dropdown menus
  3. Process data from other cells and generate insights or summaries
  4. Export longer AI-generated content directly to Google Docs with automatic linking
  5. Save AI responses as text files in Google Drive
  6. Include images in prompts for vision-enabled models
  7. Use templated prompts that reference cell values, web content, or Google Docs
  8. Generate multiple AI responses in batch by applying the function to ranges
  9. Place AI responses in specific target cells across your spreadsheet

Syntax and Parameters

=TASK_LM(header, prompt, model, [output_mode], [output_doc_name], [output_textfile_name], [celladdress], [image_url])

Parameter Data Type Required Description
header string No Column header for the task template. This appears in the first row of the output.
prompt string Yes The input prompt/template for the language model. Supports cell references {A1}, column references {A} (current row), URLs {https://example.com}, document IDs {docId}, and dictionary lookups {A1:A5,B1:B5}.
model string Yes The model name in the format 'provider/model_name' (e.g., "Gemini/gemini-1.5-pro", "OpenAI/gpt-4", "Anthropic/claude-3-5-sonnet").
output_mode string No How to display the result: "text" (default) for text in the cell, "list" for vertical list, "hlist" for horizontal list, "dropdown" for dropdown selection, "google_doc" for Google Doc with link, "gdrive_textfile" for text file with link, "other_cell" to target a specific cell.
output_doc_name string No The name of the Google Doc to create (when output_mode="google_doc").
output_textfile_name string No The name of the text file to create (when output_mode="gdrive_textfile").
celladdress string No The cell address for output (e.g., "C1"). When using output_mode="other_cell", this parameter is required. Otherwise, the cell to the right of the function is used.
image_url string No The URL of an image to include with the prompt. Only works with vision-capable models.

Advanced Options

Template Syntax

Include dynamic content in prompts with these placeholder formats:

  • Cell references: {A1} or {Sheet2!B3} - Inserts the value of the specified cell
  • Column references: {Url} {A} or {B} - Inserts the value from the current row in the specified column by column header or column letter
  • URLs: {https://example.com} - Fetches and inserts web content
  • URLs with formatting: {https://example.com|markdown} - Fetches and formats web content
  • Google Docs: {docId} - Inserts Google Doc content
  • Dictionary lookups: {A1:A5,B1:B5} - Creates key-value pairs from two ranges

Output Modes

  • text: Places the response as plain text in the output cell
  • list: Converts the response into a vertical list in a column
  • hlist: Converts the response into a horizontal list in a row
  • dropdown: Creates a dropdown selection menu with options from the response
  • google_doc: Creates a Google Doc with the response and places a hyperlink in the output cell
  • gdrive_textfile: Creates a text file with the response and places a hyperlink in the output cell
  • other_cell: Places the response in a specific cell address

Vision Support

When using vision-capable models like Gemini or GPT-4 with vision:

  • Set the image_url parameter to a publicly accessible image URL
  • The image will be included alongside your prompt text
  • Useful for image analysis, chart interpretation, or visual question answering

File Output Organization

  • Google Docs are stored in the Sheetworkflows/Docs folder in your Drive
  • Text files are stored in the Sheetworkflows/Files folder
  • Both create hyperlinks in the output cell for easy access

Examples

Generate a summarized paragraph

=TASK_LM("Summary", "Summarize the quarterly results: {A2} in a concise paragraph.", "Anthropic/claude-3-5-sonnet")

A concise paragraph summary appears in the cell to the right of the formula.

Create a dropdown list of options

=TASK_LM("Options", "Generate a list of 5 potential project names based on our product: an AI-powered spreadsheet assistant.", "OpenAI/gpt-4", "dropdown")

A dropdown menu appears with 5 AI-generated project name options.

Generate a document with meeting minutes

=TASK_LM("Meeting Notes", "Based on the agenda in cell {A} and discussion points in {B}, generate comprehensive meeting minutes in a professional format.", "Anthropic/claude-3-5-sonnet", "google_doc", "Team Meeting Minutes")

A hyperlink to a Google Doc named "Team Meeting Minutes" appears in the output cell.

Generate a response to an image

=TASK_LM("Image Analysis", "What does this chart show? What are the key trends? Provide 3 insights.", "Gemini/gemini-1.5-pro", "text", , , , "https://example.com/chart.jpg")

The cell will display the AI's analysis of the chart image.

Export code to a text file

=TASK_LM("Python Code", "Write a Python script that analyzes stock data and generates a moving average chart.", "OpenAI/gpt-4", "gdrive_textfile", , "stock_analysis.py")

A hyperlink to a text file named "stock_analysis.py" appears in the output cell.

Place result in a specific cell

=TASK_LM("Analysis", "Analyze the sales trend: {Sales} and explain the pattern.", "Anthropic/claude-3-5-sonnet", "other_cell", , , "H5")

The analysis appears in cell H5, regardless of where the formula is placed.

Best Practices

  • Select empty cells in the column and click the Run button in the Sheet Workflows sidebar after entering the function to execute it
  • Use headers that clearly describe the task for better organization in templates
  • Keep prompts clear and specific to get better results from language models
  • For lists, ensure your prompt asks for a list of items
  • Add parameters like output_doc_name only when needed; leave others blank with commas as placeholders
  • For large responses, use "google_doc" output mode to avoid cell size limitations
  • Use column-specific prompts with templates when applying to multiple rows (e.g., {A} references current row)
  • Avoid very long prompts that may exceed model token limits
  • When using image_url, ensure the image is publicly accessible or from Google Drive with proper permissions

Troubleshooting

Error: Invalid model format

Cause: The model name doesn't follow the provider/model_name format.
Solution: Use the correct format like "OpenAI/gpt-4" or "Gemini/gemini-1.5-pro".

Error: Cell address not found

Cause: The specified celladdress doesn't exist or is invalid.
Solution: Check the cell address format and ensure it exists in the spreadsheet.

Error: API call failed

Cause: The language model API returned an error or timed out.
Solution: Check your API configuration, internet connection, or try again later.

Error processing prompt template

Cause: Placeholders in the prompt couldn't be resolved.
Solution: Ensure all referenced cells, URLs, or documents exist and are accessible.

No dropdown appears

Cause: The model didn't return a proper list format for the dropdown.
Solution: Modify your prompt to explicitly request a list of items.

FAQ

How do I access different AI models in the model parameter?

To use different AI models, format the model parameter as "Provider/model-name" (e.g., "OpenAI/gpt-4", "Anthropic/claude-3-5-sonnet", "Gemini/gemini-1.5-pro"). Configure your API keys in the Sheet Workflows sidebar first.

Can I reference values from other cells in my prompts?

Yes, use the syntax {A1} (cell address) to reference specific cells or {A} or {Article} (by column heading) to reference the value in the current row of column A. You can also reference ranges with {A1:A10} or create dictionaries with {A1:A5,B1:B5}.

How do I get the language model response into multiple cells?

As the model for a list of items in your prompt and use output_mode="list" for vertical lists or output_mode="hlist" for horizontal lists. The AI response will be parsed and placed in consecutive cells.

Is there a character limit for cell outputs?

Google Sheets cells have a limit of approximately 50,000 characters. For longer outputs, use output_mode="google_doc" to create a Google Doc with the full response.

How do I include an image in my prompt?

Provide a public URL to the image in the image_url parameter. This only works with vision-capable models like Gemini or GPT-4 with vision capabilities.

How do I run the function after entering it?

After entering the TASK_LM function, select the cell and click the "Run" button in the Sheet Workflows sidebar. The function itself only generates the task definition JSON.

Can I process multiple rows at once?

Yes, create a template in row 1 using TASK_LM, then select multiple cells in that column and click "Run" in the sidebar. Each row will be processed using its own row-specific context.

Where are the created Google Docs and text files stored?

Google Docs are stored in the "Sheetworkflows/Docs" folder and text files in the "Sheetworkflows/Files" folder in your Google Drive.

Can I use formatting in the AI responses?

The responses are plain text or markdown by default. For formatted responses, use output_mode="google_doc" which preserves markdown formatting in the created document.

How do I update an existing Google Doc with new AI content?

Each run creates a new document. To update content, either manually update the existing document or use TASK_APPEND with the docId of the existing document.

Does this function work with all language models?

It works with all configured providers in Sheet Workflows, including OpenAI, Anthropic, Gemini, Groq, and others. You need to configure API keys for each provider you want to use.

Can I save the response to a specific cell instead of the one to the right?

Yes, use output_mode="other_cell" and specify the target cell with the celladdress parameter.

Elisha

1 week ago

Categories
Features