Kết Nối API Với IMPORTDATA/IMPORTXML

Thời gian đọc: ~35 phút

IMPORTDATA — Import CSV/TSV

=IMPORTDATA("https://example.com/data.csv")
=IMPORTDATA("https://api.example.com/export?format=csv")

IMPORTXML — Lấy Dữ Liệu Web

=IMPORTXML("https://example.com", "//title")
' Lấy thẻ title

=IMPORTXML("https://example.com/feed.xml", "//item/title")
' Lấy title của RSS feed

=IMPORTXML("https://vietstock.vn/2024/01/vn-index.htm", "//span[@class='price']")
' Lấy giá chứng khoán (ví dụ)

IMPORTFEED — RSS/Atom

=IMPORTFEED("https://example.com/feed.xml", "items title", FALSE, 10)
' Lấy 10 tiêu đề bài viết mới nhất

IMPORTHTML — Bảng Web

=IMPORTHTML("https://vi.wikipedia.org/wiki/Việt_Nam", "table", 1)
' Import bảng đầu tiên từ Wikipedia
Giới hạn IMPORT functions:
- Tự động refresh mỗi vài giờ (không real-time)
- Tối đa 50 hàm IMPORT mỗi spreadsheet
- Website có anti-scraping có thể block

Apps Script Cho JSON API

Với JSON API, dùng Apps Script (xem khóa Apps Script A-Z):

function importJSON() {
  const res = UrlFetchApp.fetch('https://api.example.com/data');
  const data = JSON.parse(res.getContentText());
  SpreadsheetApp.getActiveSheet().getRange('A1').setValue(data.value);
}
Tổng kết GS Nâng Cao: Bạn đã master QUERY, ARRAYFORMULA, LAMBDA, Pivot Table, IMPORTRANGE, Sparkline Dashboard và API. Tiếp theo: Apps Script A-Z để tự động hóa hoàn toàn!