Vibe Coding in Action: Building Your Exchange Rate Crawler Bot from Scratch

In the previous chapter, we successfully opened the Google Apps Script development interface. Now, we'll temporarily set this aside and open our most powerful development companion: Cursor IDE (or ChatGPT, Claude).

In traditional IT training courses, instructors would typically start writing obscure jargon on the blackboard at this point: What is the UrlFetchApp class? What is JSON.parse? What is SpreadsheetApp.getActiveSpreadsheet()?

But in the world of Vibe Coding (Incantation Development), you don't need to memorize any of this!
You only need to know one thing: "How to precisely describe your business requirements to AI in Chinese."


Crafting the Perfect Vibe Prompt (Atmospheric Incantation Spell)

To get AI to write functional GAS code in one attempt, our spells can't be too vague (e.g., don't just say: "Help me write an exchange rate fetching program"). The perfect spell must contain three key elements:

  1. Identity and Environment Setup: Tell AI this runs on Google Apps Script, which significantly reduces the chance of AI writing Node.js or Python code.
  2. Input Source: Where should it fetch data from? Are there specific APIs?
  3. Output Target: How should the fetched data be placed in Google Sheets? Which column?

Here's your first basic spell. Feel free to copy it and paste it to your AI tool:

【Copy This Basic Prompt Spell】 You are now a professional Google Apps Script (GAS) developer. I need you to write a function that accomplishes the following tasks:

  1. Use GAS's built-in UrlFetchApp to call a free public exchange rate API (e.g., exchangerate-api) targeting the real-time exchange rate between USD and TWD.
  2. Parse the fetched exchange rate data as JSON.
  3. Use SpreadsheetApp.getActiveSpreadsheet().getActiveSheet() to get the currently open spreadsheet.
  4. Write the "current execution date and time" to the last row of Column A in the spreadsheet (appendRow).
  5. Write the parsed "USD exchange rate number" to the last row of Column B.
  6. Include complete try-catch error handling to prevent fetch failures, and add clear Traditional Chinese comments.

🌟 Advanced Scenario Prompt Spell Library (Bonus)

To help you expand your skills, we've prepared two additional spells for other business scenarios. If you don't want to fetch exchange rates, try these instead!

Scenario A: Stock Tracking Automation Spell

【Stock Tracking Prompt】 You are now a professional Google Apps Script (GAS) developer. I need you to write a script that:

  1. Uses UrlFetchApp to fetch data from Taiwan Stock Exchange's public API or Yahoo Finance API to get TSMC's (2330) closing price today.
  2. Writes the "current date" to the last row of Column A in Google Sheets.
  3. Writes "TSMC's closing price" to the last row of Column B.
  4. (Advanced) Check the cost price in Column C. If the closing price in Column B falls below the cost price in Column C, use MailApp.sendEmail to automatically send an alert email to my inbox.

Scenario B: Daily Weather Assistant Spell

【Weather Auto-Recording Prompt】 You are now a professional Google Apps Script (GAS) developer. Please write a script that:

  1. Calls the Central Weather Administration's open data API (I'll add the key later) to fetch tomorrow's rain probability and maximum temperature for Taipei City.
  2. Gets the currently active Google Sheet.
  3. Writes the date to Column A, temperature to Column B, and rain probability to Column C.
  4. Add detailed Chinese comments explaining where to insert the API key later.

Pasting the Code Back into GAS and Executing

Assuming you used the basic exchange rate spell, AI will typically generate code similar to the following (variable names may vary slightly between AI generations, which is normal):

function fetchExchangeRate() {
  try {
    // 1. Set up a no-key-required exchange rate API URL (USD as base)
    const apiUrl = "https://open.er-api.com/v6/latest/USD";
    
    // 2. Send HTTP request via UrlFetchApp
    const response = UrlFetchApp.fetch(apiUrl);
    const json = JSON.parse(response.getContentText());
    
    // 3. Get Taiwan Dollar (TWD) rate from JSON
    const twdRate = json.rates.TWD;
    
    // 4. Get current time and format as string
    const now = new Date();
    const timeString = Utilities.formatDate(now, Session.getScriptTimeZone(), "yyyy-MM-dd HH:mm:ss");
    
    // 5. Get currently active Google Sheet
    const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
    
    // 6. Append time and rate as an array to the bottom of the sheet
    sheet.appendRow([timeString, twdRate]);
    
    // 7. Log success message in GAS execution log
    Logger.log("Success! Latest rate written: " + twdRate);
    
  } catch (error) {
    // Error handling: Log if network fails or API is down
    Logger.log("Critical error occurred: " + error.toString());
  }
}

Key Steps:

  1. Copy all AI-generated code.
  2. Return to your Google Apps Script editor tab.
  3. Delete the default function myFunction() { ... } and paste the new code.
  4. Click the Save (disk icon) at the top (or press Ctrl+S / Cmd+S).
  5. Ensure the dropdown menu shows fetchExchangeRate function, then click Run.

🚨 First-Run Permission Review (Newbie's Scary Moment)

When you first click "Run," Google will show an "Authorization Required" warning because your script accesses spreadsheets and external websites. Many beginners panic here—follow these steps:

  1. Click Review Permissions in the popup.
  2. Select your Google account.
  3. You'll see a red triangle saying "Google hasn't verified this app." Don't worry! This app was written by AI minutes ago, so of course Google hasn't verified it.
  4. Click the gray Advanced at bottom left.
  5. Click Go to 'Untitled project' (unsafe) at the bottom.
  6. Review permissions (e.g., edit spreadsheets, connect to external services) and click Allow.

Once approved, your script will run!


Check Your Spreadsheet! Witness the Automation Magic

Switch back to your "Auto Exchange Rate Tracker" spreadsheet. You'll magically see:

  • Column A: Current timestamp
  • Column B: Precise USD exchange rate

Click "Run" again in GAS to append new data. Congratulations—you've built your first automated crawler script! With Vibe Coding, you don't even need to understand JSON.parse to summon powerful cloud automation tools.


🛠️ Common Errors & AI Debugging Guide (Troubleshooting)

Sometimes AI makes mistakes. If you see red error messages in the Execution Log, use this debugging mantra:

Mantra: Don't fix it yourself—ask AI!

Copy the red error message (including "Exception" etc.) and paste it back to Cursor/ChatGPT with this debug spell:

【Super Debug Prompt】 When running the code in Google Apps Script, I encountered this error: [Paste your red error here, e.g.: TypeError: Cannot read properties of undefined (reading 'TWD')] What caused this? Is the API URL dead? Wrong parsing structure? Please fix the error and provide updated, guaranteed-to-work code.

AI will usually realize: "Ah! Sorry, this API's response structure changed..." and give you a corrected version. This is the beauty of coding with AI.

For advanced use, click the "clock icon (Triggers)" to set the script to auto-run daily at 8 AM. You've now got a free, tireless robot assistant!

Member Exclusive Free Tutorial

This chapter is free exclusive content for registered members! Please login or register to unlock immediately.

Login / Register Now