នេះគឺជាវិធីសម្រាប់ធ្វើ spreadsheet ឬ google sheet ធ្វើជា API ប្រើជាមួយ node js បង្ហាញនៅ web app
ដំណាក់កាលទី១
មុនដំបូងចូលទៅបង្កើត project នៅ console.cloud.google
ដាក់ឈ្មោះ project

នេះគឺជាវិធីសម្រាប់ធ្វើ spreadsheet ឬ google sheet ធ្វើជា API ប្រើជាមួយ node js បង្ហាញនៅ web app
ដំណាក់កាលទី១
មុនដំបូងចូលទៅបង្កើត project នៅ console.cloud.google
ដាក់ឈ្មោះ project
បង្កើត project service
សូមយក edit យើងអាច insert និង view
email នេះយើងសូម copy ដាក់នៅក្នុង share នៃ spreadsheet
សូម download ជា jSON ហើយយើងសូមដាក់ឈ្មោះថា sheetAPI
ដំណាក់កាលទី2
យើងទៅ enable google sheet API ជាមួយឈ្មោះ project របស់យើង។ យើងសូមចូលទៅក្នុង console.cloud.google.com/apis/library
ដំណាក់កាលទី3
យើងបង្កើត project នៅក្នុង node js
npm init -y
npm install googleapis express
Get Data
const express = require('express') const app = express() const {GoogleAuth} = require('google-auth-library') const {google} = require('googleapis') //Variable spreadsheet const spreadsheetId = '1QkXam1eVH-KmNC55AAHkEfwZ_hfXZqQvikmFXbe5t2s'; const range = 'mySheet' //Name of sheets app.get("/", async(req, res)=>{ /*MARK: create Auth of google */ const auth = new GoogleAuth({ keyFile: "mySheetApi.json", scopes: "https://www.googleapis.com/auth/spreadsheets" }) /*MARK: create client */ const client = await auth.getClient(); const sheet = google.sheets({version:"v4", auth: client}) /*MARK: get value from spreadsheet */ await sheet.spreadsheets.values.get({ spreadsheetId, range },(err, data) =>{ res.send(data.data) }) }); app.listen(3000,()=>{ console.log("Port 3000") })
const express = require('express') const app = express() const {GoogleAuth} = require('google-auth-library') const {google} = require('googleapis') //Variable spreadsheet const spreadsheetId = '1QkXam1eVH-KmNC55AAHkEfwZ_hfXZqQvikmFXbe5t2s'; const range = 'mySheet' //Name of sheets app.get("/", async(req, res)=>{ /*MARK: create Auth of google */ const auth = new GoogleAuth({ keyFile: "mySheetApi.json", scopes: "https://www.googleapis.com/auth/spreadsheets" }) /*MARK: create client */ const client = await auth.getClient(); const sheet = google.sheets({version:"v4", auth: client}) /*MARK: get value from spreadsheet */ await sheet.spreadsheets.values.get({ spreadsheetId, range },(err, data) =>{ res.send(data.data) }) // Insert Data to spreadsheet await sheet.spreadsheets.values.append({ spreadsheetId, range, valueInputOption: "USER_ENTERED", resource :{ values:[ [6, "PPSD", "SEJKKJLF"] ] } }) res.send("Sheet updated! ") }); app.listen(3000,()=>{ console.log("Port 3000") })
const express = require("express"); const {google} = require("googleapis"); const app = express() //Variable spreadsheet const spreadsheetId = '1QkXam1eVH-KmNC55AAHkEfwZ_hfXZqQvikmFXbe5t2s'; const range = 'mySheet' //Name of sheets app.get('/', async(req,res)=>{ const auth = new google.auth.GoogleAuth({ keyFile: "mySheetApi.json", scopes: "https://www.googleapis.com/auth/spreadsheets" }); //Create Client const client = await auth.getClient() // Instance of google sheet API const sheet = google.sheets({version: "v4", auth: client}) //Get data from spreadsheet const metaData = await sheet.spreadsheets.values.get({ spreadsheetId, //range: "mySheet!A:A" range }) // Write to spreadsheet res.send(metaData.data) }) app.listen(4000, ()=>{ console.log("server port 4000") })
0 Comments