google-api-wrapper
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -31,20 +31,29 @@ const { google } = require('googleapis'); | ||
last_write_op: null, | ||
clear: async function() { | ||
await this.sheets.spreadsheets.values.clear({ | ||
spreadsheetId: this.id, | ||
range: this.range, | ||
}); | ||
}, | ||
write: function(row) { | ||
write: async function(row) { | ||
this.batch_rows = this.batch_rows || []; | ||
this.batch_rows.push(row); | ||
if(this.batch_rows.length >= this.BATCH_SIZE) { | ||
this.batch_write(this.batch_rows); | ||
await this.endWrite(); | ||
} | ||
}, | ||
endWrite: async function() { | ||
if(this.batch_rows && this.batch_rows.length) { | ||
let rows = this.batch_rows; | ||
this.batch_rows = []; | ||
await this.batchWrite(rows); | ||
} | ||
}, | ||
batch_write: async function(rows) { | ||
batchWrite: async function(rows) { | ||
let sheets = this.sheets; | ||
if(this.last_batch_write_op) { | ||
await this.last_batch_write_op; | ||
} | ||
console.log(`writing to ${this.id} ${this.range} ${rows.length}`); | ||
this.last_batch_write_op = await sheets.spreadsheets.values.append({ | ||
await sheets.spreadsheets.values.append({ | ||
spreadsheetId: this.id, | ||
@@ -60,10 +69,2 @@ range: this.range, | ||
endWrite: async function() { | ||
if(this.batch_rows && this.batch_rows.length) { | ||
let op = this.batch_write(this.batch_rows); | ||
this.batch_rows = []; | ||
await op; | ||
} | ||
} | ||
} | ||
@@ -70,0 +71,0 @@ |
{ | ||
"name": "google-api-wrapper", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Google API Wrapper", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -31,4 +31,6 @@ # Google API Wrapper | ||
sheets.set(id, range = 'Sheet1'); | ||
sheets.write(row); | ||
sheets.endWrite(); | ||
await sheets.clear(); | ||
await sheets.write(row1); | ||
await sheets.write(row2); | ||
await sheets.endWrite(); | ||
@@ -35,0 +37,0 @@ Batches up multiple rows and then write once at interval of 500 rows, or when endWrite() is called. |
5711
103
41