Ignoring ' from javascript code, SQLite - javascript

I have a french category named piqûres d'insectes that I am pulling from a SQLite database. Unfortunately, the ' in the category keeps breaking my javascript code, as seen in my pictures where it turns my breadcrumbs into undefined (half the word is missing as well clearly from the apostrophe). Is there a way I can pull this as just text so it does not break my code?
Javascript:
function txSuccessListAddSymptoms(tx,results) {
//console.log("Read Additional Symptoms success");
var category = getUrlVars().category;
var mainsymptom = getUrlVars().mainsymptom;
var len = results.rows.length;
var addSymp;
for (var i=0; i < len; i = i + 1) {
addSymp = results.rows.item(i);
};
$('#addSymps').listview('refresh');
}

You can use an escape character: \'
So for example try to use: piqûres d\'insectes
You can use the original name by adding this lines of code:
var str = getUrlVars().category;
var category = str.replace("'", "\'");
This code changes the ' to \' if it is in the name.
I hope this helped for you
EDIT::
Soo.. this would be your script:
function txSuccessListAddSymptoms(tx,results) {
//console.log("Read Additional Symptoms success");
var str = getUrlVars().category;
var category = str.replace("'", "\'");
var mainsymptom = getUrlVars().mainsymptom;
var len = results.rows.length;
var addSymp;
for (var i=0; i < len; i = i + 1) {
addSymp = results.rows.item(i);
};
$('#addSymps').listview('refresh');
}

Related

Javascript spilt function does not recognise one of the tabs in my string

I have a string with tab-separated data copied from Excel. The javascript split does not recognise the 2nd tab in the string. I have pasted the string into notepad++ to see the tabs and they are all there. Exploding the string in PHP works fine. The test code is:
function testTab(str) {
var strSplit = str.split('\t');
for (i=0; i < strSplit.length; i++){
console.log('strSplit['+i+'] = '+strSplit[i]);
}
}
The console output (where the tab between 1st and 2nd item is not recognised):
strSplit[0] = 0.02194 0.028940568
strSplit[1] = 0.05227
strSplit[2] = 0.040229885
strSplit[3] = 0.04650
strSplit[4] = 0.035630689
strSplit[5] = 0.07055
strSplit[6] = 0.015557256
strSplit[7] = 0.01960
strSplit[8] = 0.03527
strSplit[9] = 0.05276
strSplit[10] = 0.05669
strSplit[11] = 0.05680
strSplit[12] = 0.04464
strSplit[13] = 1
Unsure if the string copies correctly with all tabs, but here it is:
const str = `0.02194 0.028940568 0.05227 0.040229885 0.04650 0.035630689 0.07055 0.015557256 0.01960 0.03527 0.05276 0.05669 0.05680 0.04464 1`;
function testTab(str) {
var strSplit = str.split('\t');
for (i = 0; i < strSplit.length; i++) {
console.log('strSplit[' + i + '] = ' + strSplit[i]);
}
}
testTab(str)
Thanks for your suggestions, it inspired me to review all options.
I changed the delimiters in the regex so that the split now reads:
var strSplit = str.split(/\t/);
For some reason it now works.

Error in reading text from textarea in Javascript

In a webpage there is a textarea (id="text") and also a button (id="dlButton3").
What I have to do is to enter the text into the textarea. And when I press the button, then the following will be happened:
Text in the text area will be loaded into the function,
The text will be spitted with delimiters ""
There is a for loop to compare all the lengths of the strings, and
Print the longest one value
The problem is, with the following code, I can take the string from the text area but dun know why I cannot split the string, and it returns the error "Uncaught ReferenceError: targetString is not defined"
The code is as followed
function findlongestword(){
var testing = document.getElementById('text').value;
console.log(testing);
var strText = testing.split(" ");
var length = 0;
for (var i=0; i < strText.length; i++) {
if (length < strText[i].length) {
length = strText[i].length;
targetString = strText[i]
}
}
console.log (targetString);
}
window.onload = function(){
findlongestword();
document.getElementById("dlButton3").onclick = findlongestword;
}
What can be the error?
Many thanks for your help in advance!
Read more about block scope targetString only exists within the loop
function findlongestword() {
var testing = document.getElementById('text').value;
var strText = testing.split(" ");
var length = 0;
var targetString = '';
for (var i=0; i < strText.length; i++) {
if (length < strText[i].length) {
length = strText[i].length;
targetString = strText[i]
}
}
console.log (targetString);
}
window.onload = function() {
findlongestword();
document.getElementById("dlButton3").onclick = findlongestword;
}
"Uncaught ReferenceError: targetString is not defined"
You did not declare targetString before using it within your loop. Therefore javascript did not know where to find or 'Refer' to it after the loop ended.
By adding var targetString = ""; before the loop begins the problem will be solved.
function findlongestword(){
var testing = document.getElementById('text').value;
console.log(testing);
var strText = testing.split(" ");
//ADD HERE
var targetString = "";
var length = 0;
for (var i=0; i < strText.length; i++) {
if (length < strText[i].length) {
length = strText[i].length;
targetString = strText[i]
}
}
console.log (targetString);
}
window.onload = function(){
findlongestword();
document.getElementById("dlButton3").onclick = findlongestword;
}

If new contact get address, subject, message

I need a program for google script that tells me if I have an email address in my contacts or not.
I get many emails from vendors each day and many of them are completely new contacts. I need a program that makes a list of only the new contacts so I can decide which contact group to add them too.
Basic structure of what I'm looking for:
Will run this program every 5 minutes (I can set that up.)
-Checks emails with yellow or blue star for email address of recipient and sender. filters out my email address.
-Checks the email address against all my contacts. If the email address does not appear in my google contacts I need:
Email address, subject line, message text, additional thread message, additional thread message... in seperate columns in a spreadsheet.
I will then go through this information manually to decide which groups to put them in.
Thank you for your help!!!
UPDATE//////////////////////////////update.3
Here's where I'm at. This is working on and off. One time I run it it works, another time it doesn't. REally annoying. If anyone can see any glairing problems let me know. Especially hard to get the if sndr and if rcpnt to return false to run the rest of the program. I've tried about 20 ways!!!
//http://webapps.stackexchange.com/questions/9813/get-e-mail-addresses-from-gmail-messages-received
/////////////NESTED LOOP METHOD
function newEmailAddressList(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("NewEmails");
var range = sheet.getRange("A2:BA");
var addrA;
range.clearContent(); //May need to change this to my delete function to speed things up.
var contact = ContactsApp.getContacts();
for(var i = 0;i < contact.length;i++){
var addrA = [];
var start;
var addresses = contact[i].getEmails();
for(var j = 0;j < addresses.length;j++){
addrA.push(addresses[j].getAddress());
};
};
sheet.getRange('H1').setValue("List Created");
for (var i=0; i<50; i++){
var threads = GmailApp.getInboxThreads(0,50)[i];
var messages = threads.getMessages()[0];
var sndr = messages.getFrom().replace(/^.+<([^>]+)>$/, "$1"); //http://stackoverflow.com/questions/26242591/is-there-a-way-to-get-the-specific-email-address-from-a-gmail-message-object-in
var rcpnt = messages.getTo().replace(/^.+<([^>]+)>$/, "$1");
function contains(addrA, sndr) {
for (var i = 0; i < addrA.length; i++) {
if (addrA[i] === sndr) {
sheet.appendRow("Emails Match");
}else{
var dat = messages.getDate();
//var sndr = messages.getFrom();
//var rcpnt = messages.getTo();
var sub = messages.getSubject();
var msg = messages.getPlainBody();
var info = [dat,sndr,rcpnt,sub,msg];
sheet.appendRow(info); //appendRow only works with sheet class not range class
};
};
};
};
};
Well, I'll give you a start. And keep in my that I've never really worked with GmailApp in the past so all of this information is pretty much readily available to any who opens the code editor clicks on help and then clicks on API Reference. Everything you need to know about Google Scripts is right there organized about as well as it's ever been. They have really improved their documentation since the first time I've looked at it several years ago.
So I'm giving you some functions that will get all of your contact emails, your from emails from your inbox and and your emails.
function myContacts()
{
var s = '';
var br = '<br />';
var contact = ContactsApp.getContacts();
for(var i = 0;i < contact.length;i++)
{
var addrA = [];
var addresses = contact[i].getEmails();
for(var j = 0;j < addresses.length;j++)
{
s += addresses[j].getAddress() + br;
addrA.push(addresses[j].getAddress());
}
}
dispStatus('Contact Emails',s, 800, 400);
}
function MyFroms()
{
var threads = GmailApp.getInboxThreads();
var s = '';
for(var i = 0; i < threads.length; i++)
{
var msg = threads[i].getMessages();
for(var j = 0; j < msg.length;j++)
{
s += msg[j].getFrom() + '<br />';
}
}
dispStatus('My Messages', s , 800 , 400);
}
function MyMessages()
{
var threads = GmailApp.getInboxThreads();
var s = '';
for(var i = 0; i < threads.length; i++)
{
var msg = threads[i].getMessages();
for(var j = 0; j < msg.length;j++)
{
s += 'Message' + j+1 + '<br />';
s += msg[j].getFrom() + '<br />';
s += msg[j].getBody() + '<br />';
}
}
dispStatus('My Messages', s , 800 , 400);
}
function dispStatus(title,html,width,height)
{
var title = typeof(title) !== 'undefined' ? title : 'No Title Provided';
var width = typeof(width) !== 'undefined' ? width : 800;
var height = typeof(height) !== 'undefined' ? height : 400;
var html = typeof(html) !== 'undefined' ? html : '<p>No html provided.</p>';
var htmlOutput = HtmlService
.createHtmlOutput(html)
.setWidth(width)
.setHeight(height);
SpreadsheetApp.getUi().showModelessDialog(htmlOutput, title);
}
It's not a complete answer. But hopefully it will encourage you to jump in get your feet wet and exercise your mental muscles and bring us back at least a partially working skeleton of a program that we can help you get running.

ActiveXObject Number of rows counter

I have this piece of code:
function GetData(evt){
var adresses = new Array();
var j = 0;
var excel = new ActiveXObject("Excel.Application");
var fil = document.getElementById("file");
var excel_file = excel.Workbooks.Open(fil.value);
var excel_sheet = excel.Worksheets(1);
for(var i=2;i<500;i++){
var morada = excel_sheet.Range("E"+ i );
var localidade = excel_sheet.Range("C"+ i );
var pais = excel_sheet.Range("A"+i);
adresses[j] = (morada + ", " + localidade + ", " + pais);
j++;
}
for(var k = 0; k<j; k++) {
codeAddress(adresses[k]);
}
}
It receives an excel file and processes the data like I want. The thing is, it is very hard coded.
For instant, in this for:
for(var i=2;i<500;i++)
I am using 500, but I would like to use the number of rows in the sheet. I have already tried a few things like rows.count and whatever and I gave some alerts to see the results, but I just can't find the one who tells me the number of rows.
Anyone know how to do it?
Try that :
excel_sheet.UsedRange.Rows.Count
Does it works ?

Long string pagination done right

I want to split a long text into smaller chunks, that will act as pages.
var longText = document.getElementById('content').innerHTML;
for (i=0; i<10; i++) {
var page = longText.substring(i*100,(i+1)*100);
document.write(page + "<br /><hr />");
}
See it here on jsfiddle.
This code splits the text, but in a stupid way, cutting also words in half.
It would be far better, for example, creating substrings ending at the last space in a certain number of characters (count 100 characters, then go back to the last space).
How would you achieve it?
Second shot
Third shot
I would use:
function paginate(longText, pageSize) {
var parts = longText.split(/[ \n\t]/g);
if (parts.length == 0)
return [];
var pages = [parts.unshift()];
for (var i = 0; i < parts.length; i += 1) {
var part = parts[i];
if (part.length + pages[pages.length - 1].length < pageSize) {
pages[pages.length - 1] += " " + part;
} else {
pages.push(part);
}
}
return parts;
}
For those looking for a working answer:
<div id="long-text">Lorem ipsum [...]</div>
<script>
var splitter = function(id) {
var longText = document.getElementById(id).innerHTML;
var pageLenght = 200;
var charsDone = 0;
var htmlBefore = "<p>";
var htmlAfter = "</p>";
while (charsDone <= longText.length && (pageLenght+charsDone)<longText.length) {
var pageBox = longText.substr(lastSpace,pageLenght);
var lastSpace = charsDone + pageBox.lastIndexOf(" ");
var page = longText.substring(charsDone,lastSpace);
document.write(htmlBefore + page + htmlAfter);
charsDone = lastSpace;
}
document.write(longText.substr(lastSpace,pageLenght));
}
splitter("#long-text");
You can easily use arrays instead of writing to document.
You will also want to set your html to your needs, do it in htmlBefore and htmlAfter.
See it in action here.

Categories

Resources