appscript-sheet-client
v1.0.0
Published
Lightweight TypeScript client for Apps Script Google Sheets 'Mongo-like' API
Maintainers
Readme
appscript-sheet-client
TypeScript client for the Apps Script Google Sheets "Mongo-like" API implemented in this repo.
Install (from package folder):
npm installBuild:
npm run buildTwo usage styles are supported:
- Low-level AppScriptDatabase (direct mapping to API endpoints)
import { AppScriptDatabase } from './dist/index.js';
// Low level: call endpoints directly
const db = new AppScriptDatabase({ endpoint: 'https://script.google.com/macros/s/AK.../exec' });
await db.insertOne('users', { name: 'Alice' });
const results = await db.find('users', { name: 'Alice' });
console.log(results);- AppScriptClient (Mongo-like wrapper, recommended)
import { AppScriptClient } from './dist/index.js';
const client = new AppScriptClient('https://script.google.com/macros/s/AK.../exec');
await client.connect();
// Use a registered connection name (maps to ?db=NAME) or omit to use default
const db = client.db('prod');
const users = db.collection('users');
// Insert
await users.insertOne({ name: 'John', age: 25 });
// Find (returns cursor-like object with toArray())
const list = await users.find({}).toArray();
console.log(list);
// Bulk write
await users.bulkWrite([
{ insertOne: { document: { name: 'Bulk A' } } },
{ updateOne: { filter: { name: 'Bulk A' }, update: { age: 21 } } }
]);
await client.close();Notes:
- The client keeps API parity with Mongo but operations are proxied to your Apps Script web app.
db(name)maps to the?db=namequery parameter for selecting registered spreadsheet connections.connect()/close()are no-ops but included for compatibility.- The server's update semantics are simple replacements (no $set/$inc operators yet).
Connection helpers (via the client):
// register a named spreadsheet connection (server script properties)
await client.client.addConnection('prod', 'SPREADSHEET_ID');
// list connections
await client.client.listConnections();
// remove
await client.client.removeConnection('prod');Notes above apply: updates are simple replacements; you can request $set/$inc support if you prefer.
If you want, I can publish this package, add auth header support, or add operator support next.
ปัญหาและการแก้ไข (Edge cases & Troubleshooting)
ด้านล่างเป็นปัญหาที่พบบ่อยเมื่อเรียก Apps Script endpoint และวิธีจัดการเบื้องต้น (คำอธิบายเป็นภาษาไทย):
Network / fetch errors
- อาการ: การเรียก
fetchล้มเหลว (timeout, DNS, network unreachable) หรือเกิด exception ก่อนได้ response. - แนวทาง: ตรวจสอบการเชื่อมต่อ, URL ของ
endpointถูกต้องหรือไม่, และลองเรียกด้วย curl/Postman เพื่อตรวจสอบผลลัพธ์จากภายนอกโค้ด
- อาการ: การเรียก
Response ไม่ใช่ JSON /
res.json()ล้มเหลว- อาการ:
res.json()โยน error เพราะ body ไม่ใช่ JSON หรือมีข้อความ error ในรูปแบบ text - แนวทาง: ก่อนเรียก
res.json()ให้ตรวจres.ok/res.statusและในกรณีผิดปกติให้อ่านawait res.text()เพื่อตรวจสอบ payload จริง ตัวอย่างตรวจสอบ:
- อาการ:
const res = await fetch(url, opts);
if (!res.ok) {
const text = await res.text();
throw new Error(`HTTP ${res.status}: ${text}`);
}
const data = await res.json();HTTP status (4xx / 5xx)
- อาการ: endpoint ตอบกลับ 400/401/403/500 และอาจมี JSON บอกสาเหตุ หรือเป็น HTML จาก Apps Script error
- แนวทาง: อ่าน
res.statusและres.text()เพื่อสืบหา stack trace / message ที่ Apps Script ส่งกลับ ตรวจสอบการตั้งค่า permission ของ Apps Script (การแชร์/public access) ถ้าจำเป็นต้องใช้ auth ให้เพิ่ม header Authorization ในfetch(หรือปรับ endpoint ให้รองรับ)
CORS / Browser calls
- อาการ: เบราว์เซอร์บล็อกคำขอด้วย error เกี่ยวกับ CORS
- แนวทาง: ตรวจสอบการตั้งค่า Apps Script (ต้องเปิดเผย endpoint หรืออนุญาตต้นทางของคุณ) — สำหรับการพัฒนา อาจทดสอบด้วย curl/Node (ไม่ใช่เบราว์เซอร์) เพื่อแยกปัญหา
ขนาด payload / Quotas ของ Apps Script
- อาการ: การส่ง
insertManyหรือbulkWriteขนาดใหญ่ล้มเหลว, หรือ endpoint ตอบช้าจน timeout - แนวทาง: แบ่งเป็น batch ย่อย ๆ, ตรวจสอบ quotas ของ Apps Script (payload size, execution time). สำหรับชุดข้อมูลขนาดใหญ่ ให้พิจารณาส่งข้อมูลเป็นหลายคำขอหรือใช้บริการที่รองรับ payload ขนาดใหญ่กว่า
- อาการ: การส่ง
Query format / serialization
- อาการ: คำค้น (query
q) ไม่ทำงานตามคาด เพราะรูปแบบ JSON แตกต่างจากที่ server คาดหวัง - แนวทาง: ไลบรารีจะ JSON.stringify ค่า
qเมื่อส่งผ่าน URL param ในบางเมทอด ให้ตรวจสอบกับฝั่ง server ว่ารับรูปแบบไหน (ตัวอย่าง: field ชื่อ_idต้องเป็น string หรือ object)
- อาการ: คำค้น (query
การดีบัก (debugging)
- ปริ้น
await res.text()ถ้าres.json()ล้มเหลว เพื่อดู payload จริง - ทดสอบ endpoint ด้วย curl/Postman ก่อนเรียกจากโค้ด
- เพิ่ม logging ใน Apps Script เพื่อดูว่าคำขอเข้ามาถึงหรือไม่ และตรวจสอบข้อผิดพลาดใน Executions (Google Apps Script dashboard)
- ปริ้น
ข้อเสนอแนะการปรับปรุงในโค้ด
- เพิ่มการตรวจสอบ
res.okก่อนเรียกres.json()และโยน error ที่มีรายละเอียด (status + body) - ให้ opsi สำหรับส่ง custom headers (เช่น Authorization) ผ่าน
AppScriptDatabaseหรือ constructor options - เพิ่ม retry/backoff สำหรับคำขอที่ล้มเหลวแบบชั่วคราว (HTTP 429/5xx)
