I want to retrieve the whole number part of 1.027863, so the code/function should give me 1 as the answer.
My requirement is to provide the number of SMSs present in a string by splitting the same into blocks of 153 characters.
So if there are 307 character 307/153 = 2.0065, I would take 2 using the ParseInt() function and add 1 to the same indicating that there are 3 parts to the SMS.
However assuming there are 306 characters which is a multiple of 153, my code is adding 1 to the answer making it wrong.
Sample of what I have done :
var String = "Hello test SMS for length and split it into sections of 153 characters for Bulk SMS system to send to respective customers and indicate the count of messages. SMS file is getting generated as per attached file. After successful campaign Launch, file can be downloaded from Documents view in Campaign screen.";
var i = 0;
var pat= new RegExp("^[a-zA-Z0-9]+$");
var flg = pat.test(String);
if (flg == true) //checking the language to be English
{
var length = String.length;
if(length > 160)
{
var msgcount = length / 153;
var sAbs = parseInt(msgcount);
var sTot = sAbs + 1;
TheApplication().RaiseErrorText("Character " + length +" / 670 ( " + sTot +" msg)" );
}
}
Where RaiseErrorText is used to Display in the format:
Characters 307 / 1530 (3 msg)
Maybe there is a better way to write this. Any suggestions experts?
Instead of your:
var msgcount = length / 153;
var sAbs = parseInt(msgcount);
var sTot = sAbs + 1;
You could just go:
var sTot = Math.ceil(length / 153);
On the webpage
http://services.runescape.com/m=itemdb_rs/Armadyl_chaps/viewitem.ws?obj=19463
It lists prices for a particular item in a game, I wanted to grab the "Current guide price:" of said item, and store it as a variable so I could output it in a google spreadsheet. I only want the number, currently it is "643.8k", but I am not sure how to grab specific text like that.
Since the number is in "k" form, that means I can't graph it, It would have to be something like 643,800 to make it graphable. I have a formula for it, and my second question would be to know if it's possible to use a formula on the number pulled, then store that as the final output?
-EDIT-
This is what I have so far and it's not working not sure why.
function pullRuneScape() {
var page = UrlFetchApp.fetch("http://services.runescape.com/m=itemdb_rs/Armadyl_chaps/viewitem.ws?obj=19463").getContentText();
var number = page.match(/Current guide price:<\/th>\n(\d*)/)[1];
SpreadsheetApp.getActive().getSheetByName('RuneScape').appendRow([new Date(), number]);
}
Your regex is wrong. I tested this one successfully:
var number = page.match(/Current guide price:<\/th>\s*<td>([^<]*)<\/td>/m)[1];
What it does:
Current guide price:<\/th> find Current guide price: and closing td tag
\s*<td> allow whitespace between tags, find opening td tag
([^<]*) build a group and match everything except this char <
<\/td> match the closing td tag
/m match multiline
Use UrlFetch to get the page [1]. That'll return an HTTPResponse that you can read with GetBlob [2]. Once you have the text you can use regular expressions. In this case just search for 'Current guide price:' and then read the next row. As to remove the 'k' you can just replace with reg ex like this:
'123k'.replace(/k/g,'')
Will return just '123'.
https://developers.google.com/apps-script/reference/url-fetch/
https://developers.google.com/apps-script/reference/url-fetch/http-response
Obviously, you are not getting anything because the regexp is wrong. I'm no regexp expert but I was able to extract the number using basic string manipulation
var page = UrlFetchApp.fetch("http://services.runescape.com/m=itemdb_rs/Armadyl_chaps/viewitem.ws?obj=19463").getContentText();
var TD = "<td>";
var start = page.indexOf('Current guide price');
start = page.indexOf(TD, start);
var end = page.indexOf('</td>',start);
var number = page.substring (start + TD.length , end);
Logger.log(number);
Then, I wrote a function to convert k,m etc. to the corresponding multiplying factors.
function getMultiplyingFactor(symbol){
switch(symbol){
case 'k':
case 'K':
return 1000;
case 'm':
case 'M':
return 1000 * 1000;
case 'g':
case 'G':
return 1000 * 1000 * 1000;
default:
return 1;
}
}
Finally, tie the two together
function pullRuneScape() {
var page = UrlFetchApp.fetch("http://services.runescape.com/m=itemdb_rs/Armadyl_chaps/viewitem.ws?obj=19463").getContentText();
var TD = "<td>";
var start = page.indexOf('Current guide price');
start = page.indexOf(TD, start);
var end = page.indexOf('</td>',start);
var number = page.substring (start + TD.length , end);
Logger.log(number);
var numericPart = number.substring(0, number.length -1);
var multiplierSymbol = number.substring(number.length -1 , number.length);
var multiplier = getMultiplyingFactor(multiplierSymbol);
var fullNumber = multiplier == 1 ? number : numericPart * multiplier;
Logger.log(fullNumber);
}
Certainly, not the optimal way of doing things but it works.
Basically I parse the html page as you did (with corrected regex) and split the string into number part and multiplicator (k = 1000). Finally I return the extracted number. This function can be used in Google Docs.
function pullRuneScape() {
var pageContent = UrlFetchApp.fetch("http://services.runescape.com/m=itemdb_rs/Armadyl_chaps/viewitem.ws?obj=19463").getContentText();
var matched = pageContent.match(/Current guide price:<.th>\n<td>(\d+\.*\d*)([k]{0,1})/);
var numberAsString = matched[1];
var multiplier = "";
if (matched.length == 3) {
multiplier = matched[2];
}
number = convertNumber(numberAsString, multiplier);
return number;
}
function convertNumber(numberAsString, multiplier) {
var number = Number(numberAsString);
if (multiplier == 'k') {
number *= 1000;
}
return number;
}
How do I compare #Now to a data / time value in a document? This is what I have
var ProjectActiveTo:NotesDateTime = doc.getItemValueDateTimeArray("ProjectActiveTo")[0];
var ProjectExpired;
var d1:Date = #Now();
if (d1 > ProjectActiveTo.toJavaDate())
{
dBar.info("Today: " + d1 + " > " + ProjectActiveTo.toJavaDate());
ProjectExpired = true;
}
else
{
dBar.info("Today: " + d1 + " < " + ProjectActiveTo.toJavaDate());
ProjectExpired = false;
}
But this always seems to return false. I printed out some diagnostic messages.
Today: 1/18/13 6:02 PM < 1/20/01 5:00 PM
Obviously today is greater than 1/20/01 but this is the result of my test. Why?
I have done some searching and saw that the compare member function might be used but it returns an error for me and compare is not in the intelisense (or whatever Lotus calls it) in the design editor.
Found this little snippet on line - it should point you in the right direction
var doc:NotesDocument = varRowView.getDocument();
var d:NotesDateTime = doc.getItemValue("BidDueDate")[0];
var t:NotesDateTime = session.createDateTime("Today");
if (d.timeDifference(t) > 0 ) {
return "Overdue";
}else{
return "";
}
I've a view on which I performed a search. I would like to know how many rows this view has after this search. I tried with rows = view1.getEntryCount();
But this gives the number of lines of the "original" view, not the result of my search.
edit
The following works but isn't verry efficient.
Any better idea ?
productTest=sessionScope.get("product");
landTest=sessionScope.get("country");
var length = view1.getEntryCount();
var entries = view1.getAllEntries();
var i = 0;
var rows = 0;
var currentEntry = entries.getFirstEntry();
while(i < length){
land = currentEntry.getColumnValues().elementAt(0);
prod = currentEntry.getColumnValues().elementAt(1);
if (land == landTest & prod == productTest)
{
rows++;
}
currentEntry = entries.getNextEntry();
i++;
}
viewScope.queryString = rows; `
I found this on my blog. Try if if it still works:
var entryCount = viewData.getEntryCount();
var viewControl = getComponent( 'viewControlId' );
var rowCount = viewControl.getRowCount();
// If search is active -> rowcount, else entrycount
var count = ( viewControl.getDataSource().getSearch() ) ? rowCount : entryCount;
please try
rows = view1.getrowlines
I've written a script in google docs to use =importXML function and return the value on its own rather than leaving the function there loading on opening and every hour slowing the thing down.
Basically it uses the data in row D (hidden), sticks the formula in B2, then, overwrites B2 with the value of the formula. I then wanted to repeat this going down the list but just didn't know how - currently I've just repeated the function and changed the cell ID, which I'm aware is a travesty. Could someone guide a noob on how to do it efficiently?
function pullValues()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cellref1 = sheet.getRange("D2");
var ID = cellref1.getValue();
var apistring = "http://api.eve-central.com/api/marketstat?usesystem=30000142&typeid=" + ID;
var command = "importxml(\"" + apistring + "\", \"/evec_api/marketstat/type/sell/min\")";
var cellref2 = sheet.getRange("B2");
cellref2.setFormula(command);
var val = cellref2.getValue();
cellref2.setValue(val);
}
https://docs.google.com/spreadsheet/ccc?key=0AjZlH_sGnj6vdDU4QWdyZTVTd2E4RUFXZnVEZlZJS3c#gid=0
You have to iterate through all rows in the spreadsheet using a for loop. There are many ways to do it, the following code is one of them:
function pullValues()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var startRow = 2; // First row of data to process
var lastRow = sheet.getLastRow(); // Last row of data to process
for (var i = startRow; i <= lastRow; i++)
{
var cellref1 = sheet.getRange("D" + i);
var ID = cellref1.getValue();
var apistring = "http://api.eve-central.com/api/marketstat?usesystem=30000142&typeid=" + ID;
var command = "=ImportXML(\"" + apistring + "\", \"/evec_api/marketstat/type/sell/min\")";
var cellref2 = sheet.getRange("B" + i);
cellref2.setFormula(command);
var val = cellref2.getValue();
cellref2.setValue(val);
}
}