Scroll to Top
💻
Free Code
Users get ready-to-use code at no cost.
📋
Easy Copy
Copy and use the code instantly.
Quick Learning
Understand concepts fast and clearly.
📝
Step-by-Step
Follow simple instructions to implement.
📅 August 16, 2024 💻 Tutorial ⭐ Beginner Friendly

Unlock Claude 3 AI Brilliance with Free Apps Script into sheets

Author Avatar

Ashish Dwivedi

Editorial Team • Tech Writer

About This Tutorial

Unlock Claude 3 AI’s brilliance in Google Sheets using a free Apps Script. Connect to Claude’s API, send prompts directly from your sheet, and receive intelligent responses, summaries, or content ideas. Perfect for writers, analysts, and marketers looking to automate creative or analytical tasks without leaving their spreadsheet workspace.
  1. Step 1: Click on the Copy button to copy the code snippet.
  2. Step 2: Paste the copied code into your project’s script editor.

Apps Scripts Blog

Read Blog

📂 javascript
⚡ script1.js
⚡ script1.js
// ------------------------------
// Claude AI onEdit Trigger Script
// ------------------------------

var api_key = "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";

function onEdit(e) {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var show_error = 1; // Set to 0 to suppress error logging

  // ------------------------------
  // Set headers in A1 and B1
  // ------------------------------
  sheet.getRange("A1")
       .setValue("Type Claude Prompt")
       .setFontWeight("bold")
       .setFontSize(14);

  sheet.getRange("B1")
       .setValue("Claude Response")
       .setFontWeight("bold")
       .setFontSize(14);

  // ------------------------------
  // Get prompt from cell A2
  // ------------------------------
  var prompt = sheet.getRange("A2").getValue();

  // ------------------------------
  // Claude API endpoint and payload
  // ------------------------------
  var end_point = 'https://api.anthropic.com/v1/complete';

  var payload = {
    "model": "claude-v1",
    "prompt": "\n\nHuman: " + prompt + "\n\nAssistant:",
    "max_tokens_to_sample": 100,
    "temperature": 1,
    "stop_sequences": ["\n\nHuman:", "\n\nAssistant:"]
  };

  var options = {
    "method": "post",
    "headers": {
      "Content-Type": "application/json",
      "x-api-key": api_key,
      "anthropic-version": "2023-06-01"
    },
    "payload": JSON.stringify(payload),
    "muteHttpExceptions": true
  };

  // ------------------------------
  // Send request to Claude API
  // ------------------------------
  try {
    var response = UrlFetchApp.fetch(end_point, options);
    var responseData = JSON.parse(response.getContentText());

    // Extract completion text
    var completionText = responseData.completion;

    // Log for debugging
    Logger.log(completionText);

    // Write completion to cell B2
    sheet.getRange("B2").setValue(completionText);

    return response;

  } catch (e) {
    if (show_error === 1) {
      Logger.log("Error: " + e);
    }
  }
}