-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_api_tool.ts
62 lines (55 loc) · 1.85 KB
/
google_api_tool.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { GoogleAPI } from "https://deno.land/x/google_deno_integration/mod.ts";
import { GoogleCertData } from "./dataTypes.ts";
import {Logger} from "./logger.ts";
class GApi {
gapi;
constructor(){
const certStr = Deno.readTextFileSync("./app/gapi_auth.json");
const certData: GoogleCertData = JSON.parse(certStr);
this.gapi = new GoogleAPI({
email: certData.client_email,
scope: ["https://www.googleapis.com/auth/spreadsheets"],
key: certData.private_key,
});
}
// spreadsheetの指定のセルに値を書き込む
public setValue(id: string, sheet: number, row: number, column: number, value: string | number){
const endpoint = `https://sheets.googleapis.com/v4/spreadsheets/${id}:batchUpdate`
let settingValue;
if(typeof value == "string"){
settingValue = {
stringValue: value
}
}else{
settingValue = {
numberValue: value
}
}
const requestData = {
requests:[
{
updateCells:{
rows:[
{
values:[
{
userEnteredValue: settingValue
}
]
}
],
start:{
rowIndex: row,
columnIndex: column,
sheetId: sheet
},
fields: "*"
}
}
]
}
const response = this.gapi.post(endpoint,requestData);
return response;
}
}
export {GApi}