
Google Sheets เป็นเครื่องมือทรงพลังสำหรับงานร่วมมือและทีมเวิร์คจัดการและวิเคราะห์ข้อมูล สามารถผสาน Google Sheets API เพื่อ Automation งานอย่าง Data Entry, Summarization, Report Generation, Real-time Data Synchronization บทความนี้สอน Google Sheets API Integration สำหรับ Automation
ขั้นตอนที่ 1: เปิดใช้งาน Google Sheets API
Sign in to Google Cloud Console
- ไปที่ Google Cloud Console
- Login ด้วย Google Account
Create a New Project
- จาก Project Dropdown มุมซ้ายบน (ข้าง “Google Cloud”) >> New Project
- ใส่ Project Name (เช่น “Google Sheets Automation”)
- คลิก Create

เปิด API Library:
- ใน Cloud Console ไป APIs & Services >> Library
ค้นหา Google Sheets API:
- พิมพ์ Google Sheets API ใน Search Bar
- คลิกเลือก แล้ว Enable

ขั้นตอนที่ 2: ตั้งค่า API Credentials
- Go to Credentials
- ใน Cloud Console ไป APIs & Services >> Credentials
- Create Credentials
- คลิก Create Credentials
- เลือก Service Account จาก Dropdown

- Set Up the Service Account
- ใส่ Name สำหรับ Service Account
- คลิก Done

- Generate a Key for the Service Account
- ในรายการ Service Accounts คลิกที่ตัวที่เพิ่งสร้าง
- ไปแท็บ Keys >> Add Key >> Create New Key

-
- เลือก JSON คลิก Create
- ไฟล์ชื่อ credentials.json จะดาวน์โหลด เก็บไฟล์นี้ไว้ให้ปลอดภัย

ขั้นตอนที่ 3: แชร์ Google Sheet ให้ Service Account
Open Your Google Sheet
- สร้าง Google Sheet ใหม่ หรือเปิดที่มีอยู่
Share with the Service Account Email
- กลับไป Cloud Console หา Service Account
- Copy Email ของ Service Account
- ใน Google Sheet คลิก Share (มุมขวาบน)
- วาง Email Service Account คลิก Done

ขั้นตอนที่ 4: ติดตั้ง Python Libraries ที่จำเป็น
ต้องติดตั้ง google-auth และ google-api-python-client เพื่อเรียกใช้ Google Sheets API
- เปิด Terminal (Command Prompt, PowerShell หรือ Terminal ใดๆ)
- รันคำสั่ง:
pip install --upgrade google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client
จะติดตั้ง Libraries ที่จำเป็นสำหรับ API

ขั้นตอนที่ 5: Authenticate กับ Google Sheets API
รัน Python Script สำหรับ Google Sheets API Integration ใน Python Environment ใดๆ เราจะใช้ Visual Studio Code
Open Visual Studio Code:
- เปิดโฟลเดอร์ที่เก็บ credentials.json
- สร้าง Python file ชื่อ sheets_api.py ใส่โค้ด:
from google.oauth2 import service_account from googleapiclient.discovery import build # Load credentials from the JSON file credentials = service_account.Credentials.from_service_account_file( 'credentials.json', # Path to your downloaded credentials.json file scopes=['https://www.googleapis.com/auth/spreadsheets'] ) # Build the Sheets API service service = build('sheets', 'v4', credentials=credentials) print("Google Sheets API is connected!") - รัน Script คำสั่ง:
ถ้าเชื่อมต่อสำเร็จ จะเห็นข้อความ: “Google Sheets API is connected!”

ขั้นตอนที่ 6: Automation Tasks
Automate Read, Write, Append ฯลฯ ผ่าน API
1. Read Data from Google Sheets
อ่านข้อมูลจาก Google Sheets เพิ่มใน Script:
#Read data from Google Sheets spreadsheet_id = 'your-spreadsheet-id' # Replace with your actual sheet ID range_name = 'Sheet1!A1:G71' # Specify the range to read # Retrieve data from the sheet result = service.spreadsheets().values().get( spreadsheetId=spreadsheet_id, range=range_name ).execute() values = result.get('values', []) print("Data from Google Sheet:", values) - แทนที่ ‘your-spreadsheet-id’ ด้วย ID จริงของ Google Sheet
- หาได้จาก URL: https://docs.google.com/spreadsheets/d/your-spreadsheet-id/edit#gid=0
- รัน Script อีกครั้ง จะเห็นข้อมูลที่ดึงมาใน Terminal

2. Write Data to Google Sheets
เขียนข้อมูลลง Google Sheets ผ่าน Python Script และ API เพิ่มใน Script:
#Write data to the Google Sheet data = [ ['Name', 'Age', 'Department', 'Salary'], ['Alice', 30, 'HR', 60000], ['Bob', 25, 'Engineering', 70000], ['Charlie', 35, 'Sales', 80000], ] body = { 'values': data } service.spreadsheets().values().update( spreadsheetId=spreadsheet_id, range='Sheet2!A1', valueInputOption='RAW', body=body ).execute() - รัน Script จะเห็นข้อมูลอัปเดตใน Google Sheet

Schedule Script สำหรับ Automation
Automate Script ด้วย Scheduler หรือใช้ App Script เพิ่ม Trigger ตั้งเวลา
- Windows: ใช้ Task Scheduler
- macOS/Linux: ใช้ cron jobs
ตัวอย่าง Automation:
- ดึงข้อมูลจากหลายแหล่งสร้าง Daily Summaries อัตโนมัติ
- Fetch Raw Data, Clean แล้ว Write Back ข้อมูลที่ Clean แล้ว
- Sync ข้อมูลจาก External Database ไป Google Sheets
สรุป
ด้วย Google Sheets API สามารถ Automation งานต่างๆ รวมถึงงานซ้ำ Data Entry, Update Records, ผสาน Sheets กับบริการอื่นๆ Setup นี้ทำให้ Workflow ราบรื่น ลดความพยายามด้วยมือ เริ่มทดลองและฝึก API Integration เพื่อปลดล็อกศักยภาพใหม่ๆ
Get FREE Advanced Excel Exercises with Solutions!