AI Model Wiki

    Skip to main content
    10 min
    Lesson 3 of 4

    Apps Script AI Integration

    From the course Google Sheets AI Formulas

    For power users, Google Apps Script lets you build custom AI integrations directly into Google Sheets.

    Calling AI APIs from Apps Script

    Basic OpenAI Integration

    function askAI(prompt) {
      const response = UrlFetchApp.fetch(
        'https://api.openai.com/v1/chat/completions',
        {
          method: 'POST',
          headers: {
            'Authorization': 'Bearer YOUR_API_KEY',
            'Content-Type': 'application/json'
          },
          payload: JSON.stringify({
            model: 'gpt-4o-mini',
            messages: [{role: 'user', content: prompt}],
            temperature: 0.3
          })
        }
      );
      return JSON.parse(response).choices[0].message.content;
    }
    

    Custom Menu with AI Features

    function onOpen() {
      SpreadsheetApp.getUi()
        .createMenu('AI Tools')
        .addItem('Summarize Selection', 'summarizeSelection')
        .addItem('Classify Rows', 'classifyRows')
        .addItem('Generate Descriptions', 'generateDescriptions')
        .addToUi();
    }
    

    Practical Workflows

    Batch Processing

    • Process hundreds of rows through AI
    • Add progress tracking
    • Handle rate limits with delays
    • Cache results to avoid reprocessing

    Triggered Automations

    • On form submission: AI categorizes and routes
    • On edit: AI validates and suggests corrections
    • Time-based: Daily AI report generation

    Cost & Performance Tips

    • Use GPT-4o-mini for simple tasks (10x cheaper)
    • Batch requests when possible
    • Cache AI responses for identical inputs
    • Apps Script has a 6-minute execution limit — process in batches
    • Store API keys in Script Properties, not in code

    Limitations

    • Apps Script execution time: 6 minutes max
    • URL Fetch quota: 20,000 calls/day
    • AI API costs can add up with large datasets
    • Complex prompts may need multiple API calls

    Key Takeaways

    • •Apps Script enables custom AI API integrations directly in Google Sheets
    • •Build custom menus with AI features like summarization and classification
    • •Use triggered automations for on-edit validation and scheduled AI reports
    • •Store API keys in Script Properties and respect the 6-minute execution limit
    0 of 4 lessons marked complete

    Progress is kept in this browser only.