This Google Apps Script add-on integrates Perplexity AI directly into Google Sheets, allowing you to generate text, answer questions, summarize content, and perform AI-powered tasks without leaving your spreadsheet.

What’s New?

  • Menu-Based Settings: No more settings sheet—everything is now managed through the “Perplexity API” menu in Google Sheets.
  • New Model Support: Added sonar-reasoning, a model designed for enhanced reasoning and problem-solving.
  • Clarified Data Handling: Perplexity AI has publicly stated that their DeepSeek-powered reasoning models are hosted in the United States and that they do not train on user data.

What is the Perplexity API Add-on?

This add-on provides seamless access to Perplexity AI’s models via a simple function in Google Sheets:

=PERPLEXITY("Your prompt here")

Use it to: ✅ Automate tasks (generate reports, summarize data, etc.)
Generate content (blog ideas, marketing copy, email drafts)
Retrieve quick answers (factual responses, explanations, definitions)


Installation

  1. Open a Google Sheet.
  2. Click Extensions > Apps Script.
  3. Copy and paste the Perplexity API Apps Script code (provided below) into the editor.
  4. Click the disk icon to save.
  5. Refresh your Google Sheet.

After refreshing, a new “Perplexity API” menu will appear in the toolbar.


Setting Your Perplexity API Key

Before using the add-on, you must obtain an API key from Perplexity AI.

  1. Get an API Key:

    • Visit Perplexity AI.
    • Sign up or log in.
    • Generate an API key from your account settings.
  2. Enter Your API Key in Google Sheets:

    • Click “Perplexity API” > “Set API Key”.
    • Paste your API key.
    • Click OK.

Your API key is stored securely using Google Apps Script's Script Properties.


Enabling and Disabling API Calls

For security and control, you can enable or disable API calls via the Perplexity API menu.

  • Enable API Calls: Click “Perplexity API” > “Enable API Calls.”
  • Disable API Calls: Click “Perplexity API” > “Disable API Calls.”

When disabled, API requests will return an error instead of sending data to Perplexity.


Using the PERPLEXITY Function

Once the API key is set and API calls are enabled, use the PERPLEXITY function in any cell:

=PERPLEXITY("What is the capital of France?")

Advanced Usage

The PERPLEXITY function supports optional parameters:

=PERPLEXITY(prompt, [model], [maxTokens], [temperature], [topP], [frequencyPenalty], [presencePenalty])
Parameter Description
prompt (Required) The text input to send to Perplexity AI.
model (Optional) "sonar-pro", "sonar", or "sonar-reasoning". Default is "sonar".
maxTokens (Optional) Maximum response length. Higher values allow longer answers.
temperature (Optional) Controls randomness (0.0 - 2.0). Lower values = focused, higher = creative.
topP (Optional) Nucleus sampling (0.0 - 1.0). Lower values = more focused responses.
frequencyPenalty (Optional) Discourages word repetition (≥ 0).
presencePenalty (Optional) Encourages new topics (-2.0 to 2.0).

Example Queries

=PERPLEXITY("Tell me about yesterday's Apple product launch", "sonar-pro")
=PERPLEXITY("Write a poem about the ocean using references to the latest oceanography research", "sonar-reasoning", , 1.2)
=PERPLEXITY("Summarise the latest earning call from $Meta", "sonar", 50)

Troubleshooting

🔴 “Error: API Key not set” → Use Set API Key in the menu.
🔴 “Error: API calls are currently disabled” → Enable API Calls in the menu.
🔴 “Error: Rate limit exceeded” → Perplexity enforces 10 requests per minute. Wait and try again.
🔴 “Error: API returned status [number]” → Check logs: Extensions > Apps Script > Executions.


Important Notes on Perplexity AI

  • Not Affiliated: This Google Sheets add-on is independent and not officially affiliated with Perplexity AI.
  • Data Privacy: According to Perplexity AI, their DeepSeek-powered models:
    • Are hosted in the U.S.
    • Do not train on user data.
    • Do not send data to China.
    • Allow access to Chain-of-Thought (CoT) reasoning.

For more details, refer to Perplexity AI’s official documentation.


Google Apps Script Code

To install the add-on, copy and paste the following code into Google Sheets > Extensions > Apps Script:

// Global constants
const SCRIPT_PROP_API_KEY = 'API_KEY';
const SCRIPT_PROP_API_ENABLED = 'API_CALLS_ENABLED';

function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Perplexity API')
.addItem('Set API Key', 'setApiKey')
.addItem('Clear API Key', 'clearApiKey')
.addItem('Enable API Calls', 'enableApiCalls')
.addItem('Disable API Calls', 'disableApiCalls')
.addItem('About', 'showAboutDialog')
.addToUi();

const scriptProperties = PropertiesService.getScriptProperties();
if (scriptProperties.getProperty(SCRIPT_PROP_API_ENABLED) === null) {
scriptProperties.setProperty(SCRIPT_PROP_API_ENABLED, 'true');
}
}

function setApiKey() {
const ui = SpreadsheetApp.getUi();
const response = ui.prompt("Set Perplexity API Key", "Enter your API key:", ui.ButtonSet.OK_CANCEL);

if (response.getSelectedButton() === ui.Button.OK) {
const apiKey = response.getResponseText().trim();
if (apiKey) {
PropertiesService.getScriptProperties().setProperty(SCRIPT_PROP_API_KEY, apiKey);
ui.alert("API Key set successfully.");
} else {
ui.alert("Invalid API key.");
}
}
}

function PERPLEXITY(prompt, model, maxTokens, temperature, topP, frequencyPenalty, presencePenalty) {
const scriptProperties = PropertiesService.getScriptProperties();
if (scriptProperties.getProperty(SCRIPT_PROP_API_ENABLED) !== 'true') return "Error: API calls disabled.";
const apiKey = scriptProperties.getProperty(SCRIPT_PROP_API_KEY);
if (!apiKey) return "Error: API Key not set.";

const validModels = ["sonar-pro", "sonar", "sonar-reasoning"];
if (model && !validModels.includes(model)) return "Error: Invalid model. Use 'sonar-pro', 'sonar', or 'sonar-reasoning'.";

const url = "https://api.perplexity.ai/chat/completions";
const messages = [{ "role": "user", "content": prompt }];
const payload = { model: model || "sonar", messages };
if (maxTokens) payload.max_tokens = parseInt(maxTokens);
if (temperature) payload.temperature = parseFloat(temperature);
if (topP) payload.top_p = parseFloat(topP);
if (frequencyPenalty) payload.frequency_penalty = parseFloat(frequencyPenalty);
if (presencePenalty) payload.presence_penalty = parseFloat(presencePenalty);

const options = {
method: "post",
headers: { "Authorization": "Bearer " + apiKey, "Content-Type": "application/json" },
payload: JSON.stringify(payload),
muteHttpExceptions: true
};

return UrlFetchApp.fetch(url, options).getContentText();
}

Conclusion

This add-on makes Perplexity AI’s powerful models easily accessible within Google Sheets. Happy automating! 🚀

Comments