-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsheets-daemon.js
91 lines (84 loc) · 2.91 KB
/
sheets-daemon.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
var knex = require('./db/knex')
var Tabletop = require('tabletop')
function Items() {
return knex('items')
}
var public_spreadsheet_url =
'https://docs.google.com/spreadsheets/d/1xMW98m2-Y8rrbmACA9l1fUT5jb4tKvrRBCSWvH28guw/pubhtml?gid=1526253182&single=true'
Tabletop.init({ key: public_spreadsheet_url,
callback: showData,
simpleSheet: true,
parseNumbers: false,
debug: false
})
function updateDatabase(data, tabletop) {
console.log('Updating existing items')
var length = data.length
for (var i=0; i<length; i++) {
var itemFromSheet = data[i]
Items().where({ vendor_item_id: itemFromSheet.vendor_item_id })
.update({
vendor: itemFromSheet.vendor,
item_type: itemFromSheet.item_type,
vendor_item_id: itemFromSheet.vendor_item_id,
company: itemFromSheet.company,
item_description: itemFromSheet.item_description,
quantity: parseFloat(itemFromSheet.quantity),
unit: itemFromSheet.unit,
price: parseFloat(itemFromSheet.price),
unit_price: parseFloat(itemFromSheet.unit_price),
typical_origin: itemFromSheet.typical_origin,
wholesale_unit: itemFromSheet.wholesale_unit,
wholesale_unit_price: parseFloat(itemFromSheet.wholesale_unit_price)
})
.catch(function(err) {
console.log(err)
})
.then(function(results) {
console.log('Finish updating.')
console.log('Results: ', results)
})
} // for loop
} // updateDatabase declaration
function replaceDatabase(data, tabletop) {
// First, delete all rows in database.
// Put a condition in the .where() that selects all the rows.
console.log('Clearing all items from the database.')
Items().del().where({
vendor: 'Silver Monkeys'
})
.then(function(results) {
console.log(results)
})
// Then, insert all items from the spreadsheet into the database
console.log('Replacing database with google spreadsheet items.')
var length = data.length
for (var i=0; i<length; i++) {
var itemFromSheet = data[i]
Items()
.insert({
vendor: itemFromSheet.vendor,
item_type: itemFromSheet.item_type,
vendor_item_id: itemFromSheet.vendor_item_id,
company: itemFromSheet.company,
item_description: itemFromSheet.item_description,
quantity: parseFloat(itemFromSheet.quantity),
unit: itemFromSheet.unit,
price: parseFloat(itemFromSheet.price),
unit_price: parseFloat(itemFromSheet.unit_price),
typical_origin: itemFromSheet.typical_origin,
wholesale_unit: itemFromSheet.wholesale_unit,
wholesale_unit_price: parseFloat(itemFromSheet.wholesale_unit_price)
}, 'id')
.catch(function(err) {
console.log(err)
})
.then(function(results) {
console.log('Finish updating.')
console.log('Results: ', results)
})
} // for loop
} // replaceDatabase declaration
function showData(data, tabletop) {
console.log(data)
}