I have tried searching and haven't been able to see anything that can parse in JavaScript.
It would only have to parse a .txt (tab delimited) file with the following:
L123____Donald____Duck____247.09
S234____Mickey____Mouse___356.09
F456____Daffy_____Duck____1650.36
N876____Minnie____Mouse___60.45
It would have to pull out the number, first initial of first name, first initial of second name and then the number at the end (concentration). The first 3 L123, D and D could all be given a var, but the concentration number would have to get a separate one as I have to do some funky calculations and the assign it function in a liquid handling robot.
Any help on this gratefully received, even if it's only partial as I like to try and work things out ( sometimes ;-) )
I use this to test things at the moment because I don't know what else to use: https://www.w3schools.com/js/
Thank you
var input = /* your data */;
var table = input.split("\n").map(line => line.split("\t")); // here's your table
Related
I have a simple script to generate a doc and PDF upon form submission. It worked well on simple template (e.g. Only 1 sentence, First name, Last name and Company name).
However, when I use a template that's longer, having many fields, and formatting, the code runs but replace the text randomly.
I have tried to hardcode the fields of forms in ascending order as the doc template. However it still replace the text randomly
Can anybody points out what have I done wrong?
My code:
function myFunction(e) {
var response = e.response;
var timestamp = response.getTimestamp();
var [companyName, country, totalEmployees,totalPctWomenEmployees,numberNationality,name1,position1,emailAdd1,linkedin1,funFact1,name2,position2,emailAdd2,linkedin2,gameStage,gameStory] = response.getItemResponses().map(function(f) {return f.getResponse()});
var file = DriveApp.getFileById('XXXXX');
var folder = DriveApp.getFolderById('XXXXX')
var copy = file.makeCopy(companyName + '_one pager', folder);
var doc = DocumentApp.openById(copy.getId());
var body = doc.getBody();
body.replaceText('{{Company Name}}', companyName);
body.replaceText('{{Name}}', name1);
body.replaceText('{{Position}}', position1);
body.replaceText('{{Email}}', emailAdd1);
body.replaceText('{{Linkedin}}', linkedin1);
body.replaceText('{{Fun Fact}}', funFact1);
body.replaceText('{{Game Stage}}', gameStage);
body.replaceText('{{Game Story}}', gameStory);
doc.saveAndClose();
folder.createFile(doc.getAs("application/pdf"));}
My template -
Result -
Question - Does that mean the array declaration in line 3 was supposed to match the order of my form responses columns?
You can use Regular Expresion:
body.replace(/{{Company Name}}/g, companyName); // /g replace globaly all value like {{Company Name}}
Finally I found what have went wrong after so many trials and errors!
The reason is because I declared the array variables randomly without following the order of the form responses columns.
The issue is with the part -
var [companyName, country, totalEmployees,totalPctWomenEmployees,numberNationality,name1,position1,emailAdd1,linkedin1,funFact1,name2,position2,emailAdd2,linkedin2,gameStage,gameStory] = response.getItemResponses().map(function(f) {return f.getResponse()});
It's actually pulling responses from the spreadsheet, and should be corrected in order. The wrongly mapped values was what causing the replacement of text went haywire. I corrected the order as per form responses and it is all good now.
Learning points:
If you swapped around the variables, what response.getItemResponses().map(function(f) {return f.getResponse()} does is that it will go through the form responses column by column in order, and it will map the content to the wrong variable. As a result, when you replace your text later using body.replaceText('{{Game Stage}}', gameStage), there might be possibility that whatever stored in gameStage might be name1. Hence the replaced text will be wrong. And you will scratch your head until it bleeds without knowing why.
I saw #Tanaike's comment after I found the answer, but totally spot on!
I have a JSON script which contain live matches. These changes every 5 minutes. The changes could for instance be the keys live_in or score. Beside this matches are also deleted and added to the JSON. I want to keep my html output updated at all time how can i do this the best possible way? So far i've set the updating speed to 5 seconds for testing purposes. I've tried so far to set the divs id to equal to the match_id and thereby update by
$('div#match-date-id-' + match['match_id']).html('test');
However does not seem to update. How can i do this the best possible way? i've created a plnkr which enable you to download it with a json snippet, which can be edited in order to check.
plnkr.co/edit/eQCShhW01OG5jU4VLx04?p=preview
My bad earlier on :-) Now I've done more thorough testing and updated the plunker code. What I found in the test: the filter was trying to use an undefined value (match.id), also the filter was trying to compare values from different datatypes (String vs. Integer); so it wasn't working as intended:
Original:
match = match.filter(
function(match) {
return match.id > lastId;
});
Corrected code:
match = match.filter(
function(match) {
return parseInt(match.match_id) > lastId;
});
Above match_id represents String datatype in your JSON file, so I had to convert it to Integer before comparison. More info about JSON data-types. Following the older filter functionality, no match passed the comparison test.
Also for testing purposes I commented out the following line:
if (match.match_id > lastLoadedMatch) {
//lastLoadedMatch = match.match_id
}
because in the second round of updates with the same testing data, no data passes that condition.
Also, I think you need to convert match_id to Integer as well; like following:
var matchId = parseInt(match.match_id)
if ( matchId > lastLoadedMatch) {
lastLoadedMatch = match.match_id
}
I hope you find it useful :-) + note I added a timestamp for those updates; so it's clearly visible that the updates take place now every 5 seconds.
I need help with the javascript code for the following:
I created a .pdf order form. The first field on the form is the order number.
I would like to generate the order number automatically each time the .pdf file is opened. As well, I want it to increment by 1 starting from the number 1001 each time the order form is opened.
I've stored the numeric value 1001 in cell(1,1) of an excel file.
I want create a javascript to extract (Read) the current value in cell(1,1) and return that data to me.
In addition, I want the same script to overwrite the current value in cell(1,1) with value + 1.
I have searched the internet extensively and found several bits of code but none of them produce the results that I want. maybe I am doing something wrong. Help please! Below is the code I have for starters. As my programming skills are not great, I have not even certain how to incorporate the Write code. Many thanks for your help.
function readfile(){
var excel = new ActiveXObject("Excel.Application");
var excel_file = excel.Workbooks.Open("C:\Document and Settings\User\Desktop\ORDNO.xlsx");
var excel_sheet = excel_file.Worksheets("Sheet1");
var data = excel_sheet.Cells(1,1).Value = OrdNo;
return data;
}
event.value = Ordno
Excel.Quit();
I have to parse a response from the server,
The response is like..
[4,"1.0",1368544417760]
[1,"Great West Road","222",1368544595000]
[1,"Ruislip Manor Station","114",1368544479000]
[1,"Bank Station / Threadneedle Street","26",1368544731000]
[1,"Belvue School","E10",1368545955000]
[1,"Brunel Road","283",1368544706000]
[1,"Annesley Avenue","303",1368545930000]
[1,"Brixton Station Road","35",1368545854000]
[1,"Southampton Row","91",1368545537000]
[1,"Camden Road Station","29",1368545008000]
[1,"Fulham Cemetery","74",1368545210000]
The response doesn't seem to like JSON or XML.
Please help me know how to parse such type of response using Jquery.
I have to update the DOM based on the response and the response is getting updated
at a regular interval automatically.
The first number may be an indicator of what sort of data is in the rest of the "array".
I'd say
parse each line as if it were JSON. It'll turn into a javascript array.
var array = JSON.parse(oneLine); // Many browsers support this.
Then pull the bits out and put them into a proper object by name. (How to do that depends on the 1st element, perhaps.)
var obj = {};
if (array[0] == 1) {
obj.station = obj[1];
obj.number = obj[2];
obj.timestamp = obj[3]; // guessing what this is, too.
}
Do whatever you need with the data object.
Put all that in a loop. Repeat until done.
There is a similar Stack Overflow question here --> converting CSV/XLS to JSON?
Looks like there are a few different possible solutions that you could look at.
I am trying to reduce the repetition in my code but not having any luck. I reduced the code down to its simplest functionality to try and get it to work.
The idea is to take the last two letters of an id name, as those letters are the same as a previously declared variable and use it to refer to the old variable.
I used the alert to test whether I was getting the right output and the alert window pops up saying "E1". So I am not really sure why it wont work when I try and use it.
E1 = new Audio('audio/E1.ogg');
$('#noteE1').click(function() {
var fileName = this.id.slice(4);
//alert(fileName); used to test output
fileName.play();
$('#note' + fileName).addClass('active');
});
The code block works when I use the original variable E1 instead of fileName. I want to use fileName because I am hoping to have this function work for multiple elements on click, instead of having it repeated for each element.
How can I make this work? What am I missing?
Thanks.
fileName is still a string. JavaScript does not know that you want to use the variable with the same name. You are calling the play() method on a string, which of course does not exist (hence you get an error).
Suggestion:
Store your objects in a table:
var files = {
E1: new Audio('audio/E1.ogg')
};
$('#noteE1').click(function() {
var fileName = this.id.slice(4);
//alert(fileName); used to test output
files[fileName].play();
$('#note' + fileName).addClass('active');
});
Another suggestion:
Instead of using the ID to hold information about the file, consider using HTML5 data attributes:
<div id="#note" data-filename="E1">Something</div>
Then you can get the name with:
var filename = $('#note').data('filename');
This makes your code more flexible. You are not dependent on giving the elements an ID in a specific format.