-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.gs
173 lines (153 loc) · 4.27 KB
/
script.gs
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
function myTrigger() {
var ui = SpreadsheetApp.getUi();
var result = ui.prompt(
'Enter your email address .','Price alerts will be delivered to this mail' ,
ui.ButtonSet.OK_CANCEL);
var button = result.getSelectedButton();
var text = result.getResponseText();
PropertiesService.getScriptProperties().setProperty('email', text);
priceCheck();
ScriptApp.newTrigger("priceCheck")
.timeBased()
.everyHour(4)
.create();
}
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Flipkart Price Tracker')
.addItem('Initialize Price', 'initialize')
.addSeparator()
.addItem('Track Price', 'myTrigger')
.addSeparator()
.addItem('Help', 'helpDialog')
.addToUi();
}
function helpDialog() {
var html = HtmlService.createHtmlOutputFromFile('Index');
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.showModalDialog(html, 'Help for Flipkart Price Tracker');
}
function initialize()
{
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
var refcell = sheet.getRange(26,1);
for(var j=2;j<=10;j++)
{
var url= sheet.getRange(j,1).getValue();
var test= (url.indexOf("flipkart"));
var website= test>0?1:2;
refcell.setValue(url);
var selection=sheet.getRange(j,1)
sheet.setActiveSelection(selection);
if(website==1)
{
var formula1= '=importXml(A26,'
formula1=formula1.concat('"/html/body/div")')
sheet.getRange(j,3).setFormula(formula1);
var received= sheet.getRange(j,3).getValue();
Logger.log(received);
var n= (received.indexOf("Reviews"))+8;
var price="";
while (1) {
var symbol= received[n];
var chek=isNaN(symbol);
if(chek)
{
if(symbol==",")
{
price=price+symbol;
n++;
continue;
}
break;
}
else{
price=price+symbol;
n++;
}
} }
else if(website==2)
{
var formula1= '=importXml(A26,'
// formula1= formula1.concat("//span[contains(@class, 'currencyINR')])")
formula1=formula1.concat('"/html/body/option")')
sheet.getRange(j,3).setFormula(formula1);
var received= sheet.getRange(j,3).getValue();
var price="";
while (1) {
var symbol= received[n];
var chek=isNaN(symbol);
if(chek)
{
if(symbol==",")
{
price=price+symbol;
n++;
continue;
}
break;
}
else{
price=price+symbol;
n++;
}
}
}
sheet.getRange(j,3).setValue(price);
}
}
function priceCheck()
{
//showPrompt();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
var refcell = sheet.getRange(26,1);
for(var j=2;j<=5;j++)
{
refcell.setValue(sheet.getRange(j,1).getValue());
sheet.getRange(j,15).setFormula('=importXml(A26,"/html/body/div")');
var received= sheet.getRange(j,15).getValue();
var n= (received.indexOf("Reviews"))+8;
var price="";
while (1) {
var symbol= received[n];
var chek=isNaN(symbol);
if(chek)
{
if(symbol==",")
{
price=price+symbol;
n++;
continue;
}
break;
}
else{
price=price+symbol;
n++;
}
}
price=parseInt(price);
var lastPrice=sheet.getRange(j,3).getValue();
lastPrice=parseInt(lastPrice);
Logger.log(lastPrice);
var link= sheet.getRange(j,1).getValue();
//var mail = PropertiesService.getScriptProperties().getProperty('email');
Logger.log(price);
if (price>lastPrice)
{
MailApp.sendEmail({
to: PropertiesService.getScriptProperties().getProperty('email'),
subject: "Price of this product has been lowered!",
htmlBody: "<a href='" + link + "'>Link to the product</a>",
});
}
//sheet.getRange(j,5).setValue(price);
}
}
function deleteTrigger() {
// Loop over all triggers and delete them
var allTriggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < allTriggers.length; i++) {
ScriptApp.deleteTrigger(allTriggers[i]);
}
}