-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadCouponDoc.js
More file actions
198 lines (180 loc) · 5.94 KB
/
readCouponDoc.js
File metadata and controls
198 lines (180 loc) · 5.94 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//var XMLHttpRequest = XMLHttpRequest;
//'use strict';
class rowDoc {
constructor() {
this.categoria = null;
this.comeca = null;
this.termina = null;
this.sku = null;
this.produto = null;
this.link = null;
this.armazem = null;
this.preco = null;
this.cupao = null;
this.unidades = null;
this.img = null;
}
}
class LibReadCouponDoc {
constructor(_file, _api, _header = null) {
this._file = _file;
this._api = _api;
var _type = 'text/csv';
this._type = _type;
var _url = 'https://www.googleapis.com/drive/v3/files/'
this._url = _url;
var _data = null;
this._data = _data;
this._header = _header;
}
set url(url) {
this._url = url;
}
get url() {
return this._url;
}
get data() {
return this._data;
}
set header(header) {
this._header = header;
}
/**
* getFile()
* @param {*} date1
* @param {*} date2
*/
getFile(date1, date2 ) {
const url = this.url + this._file + '/export?key=' + this._api+ '&mimeType='+this._type;
const xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET", url, false);
xmlhttp.send();
var doc = xmlhttp.responseText;
this._data = doc.split('Dia ' + date1)[1].split('Dia ' + date2)[0];
return this._data;
}
/**
* getImage()
* @param {*} url
*/
getImage(url){
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET", url, false);
xmlhttp.send();
var data = xmlhttp.responseText;
var imgLink = null;
var doc = document.implementation.createHTMLDocument();
try {
doc.body.innerHTML = data;
imgLink = doc.querySelector("meta[itemprop='image']").getAttribute("content");
}catch(e) {
console.log('getImage() - error: ', e);
}
// console.log('imgLink', imgLink);
return imgLink;
//image
}
/**
* createTable() - Criar uma tabela a partir do array
* @param {*} data table array data
* @param {*} idTable id of the table
* @param {*} img boolean with img or not
*/
createTable(data, idTable, img = false) {
var table = document.getElementById(idTable);
//or use : var table = document.all.tableid;
for(var i = table.rows.length; i >= 0; i--)
{
table.deleteRow(i-1);
}
//table.deleteTHead()
if(data.length > 0 && !(table.tHead)) {
var thead = document.createElement("thead");
for (var key in data[0]) {
var th = document.createElement("th");
var txt = document.createTextNode(key);
th.appendChild(txt);
thead.appendChild(th);
}
table.appendChild(thead);
}
var tbody = document.createElement("tbody");
for (var i in data) {
var tr = document.createElement("tr");
var flag = true;
for (var key in data[i]) {
var td = document.createElement("td");
var txt = null;
if(img && key == 'img'){
txt = document.createElement("IMG");
txt.setAttribute("src", data[i][key]);
txt.setAttribute("width", "100");
txt.setAttribute("height", "100");
txt.setAttribute("alt", "");
} else
txt = document.createTextNode(data[i][key]);
if(key == 'cupao' && !(data[i][key]))
flag = false;
td.appendChild(txt);
tr.appendChild(td);
}
if(flag)
tbody.appendChild(tr);
}
table.appendChild(tbody);
}
/**
* readCSV()
* @param {*} data
* @param {*} img
*/
readCSV(data, img = false) {
var allTextLines = data.split(/\r\n|\n/);
var headers = allTextLines[0].split(',');
var lines = [];
for (var i=1; i<allTextLines.length; i++) {
var data = allTextLines[i].split(',');
if (data.length == headers.length) {
var row = new rowDoc();
for(var _i=0; _i<headers.length; _i++) {
let column = this._header[x];
if(cells[x] != null && typeof cells[x] == "string" && cells[x] !== '' && typeof row["column"] == "undefined")
row[column] = cells[x];
}
if(img && row.link != null)
row.img = this.getImage(row.link);
lines.push(row);
}
}
return lines;
}
/**
* readTable()
* @param {*} data
* @param {*} img
*/
readTable(data, img = false) {
let rows = data.split("\n");
let table = [];
for(let y in rows) {
let cells = rows[y].split("\t");
let row = new rowDoc();
let flag = true;
for(let x in cells) {
let column = this._header[x];
if(cells[x] != null && typeof cells[x] == "string" && cells[x] !== '' && typeof row["column"] == "undefined")
row[column] = cells[x];
}
if(img && row.link != null)
row.img = this.getImage(row.link);
if(flag)
table.push(row);
}
return table;
}
}
//export default LibReadCouponDoc;
/*const instance = new LibReadCouponDoc();
export { instance as LibReadCouponDoc };*/
//exports.default = LibReadCouponDoc;
exports.LibReadCouponDoc = LibReadCouponDoc;