iMacros with JS - Accessing csv file: How to change the value of n dynamically in {{!COLn}} - imacros

Am using iMacros with JS in Firefox.
To retrieve column data, I am using
SET !EXTRACT {{!COL1}}
How can I dynamically change the value 1 so that I shall retrieve data from other columns as well?
Try1:
I have set the value of n using:
iimSet("n",i);
and used like this:
SET !EXTRACT {{!COL{{n}}}}
This prints as _undefined_
Try2
I have set value like this:
iimSet("columnName","!COL"+i);
where i is iterated in for loop.
and used like this:
SET !EXTRACT {{columnName}}
This prints only the !COL1, !COL2, !COL3 as text.
PFB my complete code:
var PickPackFilePath = "D:\\";
var PickPackData = "PickPackData.csv";
var initCSV;
initCSV = "CODE:";
initCSV += "SET !DATASOURCE " + PickPackFilePath + PickPackData + "\n";
initCSV += "SET !DATASOURCE_LINE 1" + "\n";
initCSV += "SET !EXTRACT {{!COL{{n}}}}" + "\n";
var i;
for(i=1;i<=6;i++)
{
iimSet("n",i);
iimPlay(initCSV);
alert(iimGetLastExtract(1));
}
Please help.

You were very close to the right solution. Here it is:
var PickPackFilePath = "D:\\";
var PickPackData = "PickPackData.csv";
var i;
for(i=1;i<=6;i++)
{
var initCSV;
initCSV = "CODE:";
initCSV += "SET !DATASOURCE " + PickPackFilePath + PickPackData + "\n";
initCSV += "SET !DATASOURCE_LINE 1" + "\n";
initCSV += "SET !EXTRACT {{!COL" + i + "}}" + "\n";
iimPlay(initCSV);
alert(iimGetLastExtract(1));
}

Related

Google Script get a text from a cell(Spredsheet special characters like ç, à,é,è ) and search for it in a Google Doc document

I did a script (container-bound script) in my Spreadsheet in which I have 4 columns: (1) text before, (2) text after, (3) text to insert between, and (4) a URL of a Google Doc with the text in which I want to replace with the right value (between).
My method replace is not working when I have a french text (with characters like ç , à, è ) but with an english text it works fine How to solve this? Thank you very much for your help any idea is welcomed this is what I've done so far https://drive.google.com/drive/folders/1dOVNMrzEHvi3-vU3nbftK3Xoinxscrkn and my code :
/** It works for a text without accents :) but not for a french text :( **/
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Update the Google Doc") ;
var lastColumn = sheet.getLastColumn();
var numRows = sheet.getLastRow();
var COLUMN_URL = 3 ;
var data = sheet.getRange(1,1,numRows,lastColumn).getValues();
var start = 1;
var URL = data[start][COLUMN_URL];
Logger.log(' URL ' + URL);
var body = DocumentApp.openByUrl(URL).getBody();
var text_before = sheet.getRange(start + 1,1).getDisplayValue().replace(/[”|-’]/g,".");
Logger.log("text_before is " + text_before );
var text_after = sheet.getRange(start + 1,2).getDisplayValue().replace(/[”|-’]/g,".");
Logger.log("text_after is " + text_after );
var text_between = sheet.getRange(start + 1,3).getDisplayValue().replace(/[”|-’]/g,".");
Logger.log("text_between is " + text_between );
/** replace in the body of the Google Doc **/
// important to do this for the apostrophe and the " symbols that are different put the symbol in the cell
body.replaceText("\\Q’\\E","'");
// works
body.replaceText("\\Q”\\E",'"')
// ???? replace all unsupported characters from sheet means in my cell
/** symbols to test which works >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ok for the McDonald*?()\.,;%#(){!s . how about the " ***/
body.replaceText( "\\Q" + text_before + "\\E" + ".*?" + "\\Q" + text_after + "\\E", text_before + text_between + text_after );
/** another example **/
var start_bis = 2;
var text_before_bis = sheet.getRange(start_bis + 1,1).getDisplayValue().replace(/[”|-’]/g,".");
Logger.log("text_before is " + text_before_bis );
var text_after_bis = sheet.getRange(start_bis + 1,2).getDisplayValue().replace(/[”|-’]/g,".");
Logger.log("text_after is " + text_after_bis );
var text_between_bis = sheet.getRange(start_bis + 1,3).getDisplayValue().replace(/[”|-’]/g,".");
Logger.log("text_between is " + text_between_bis );
/** replace in the body of the Google Doc **/
body.replaceText( "\\Q" + text_before_bis + "\\E" + ".*?" + "\\Q" + text_after_bis + "\\E", text_before_bis + text_between_bis + text_after_bis );
}
You don't need to use replace. Just \\Q...\\E will work fine in this case. After replacing, . is considered a literal text due to (QE). Hence, it wasn't working.
Try
/** It works for a text without accents :) but not for a french text :( **/
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Update the Google Doc") ;
var lastColumn = sheet.getLastColumn();
var numRows = sheet.getLastRow();
var COLUMN_URL = 3 ;
var data = sheet.getRange(1,1,numRows,lastColumn).getValues();
var start = 1;
var URL = data[start][COLUMN_URL];
Logger.log(' URL ' + URL);
var body = DocumentApp.openByUrl(URL).getBody();
var text_before = sheet.getRange(start + 1,1).getDisplayValue();
Logger.log("text_before is " + text_before );
var text_after = sheet.getRange(start + 1,2).getDisplayValue();
Logger.log("text_after is " + text_after );
var text_between = sheet.getRange(start + 1,3).getDisplayValue();
Logger.log("text_between is " + text_between );
/** replace in the body of the Google Doc **/
// important to do this for the apostrophe and the " symbols that are different put the symbol in the cell
body.replaceText("\\Q’\\E","'");
// works
body.replaceText("\\Q”\\E",'"')
// ???? replace all unsupported characters from sheet means in my cell
/** symbols to test which works >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ok for the McDonald*?()\.,;%#(){!s . how about the " ***/
body.replaceText( "\\Q" + text_before + "\\E" + ".*?" + "\\Q" + text_after + "\\E", text_before + text_between + text_after );

iMacros nested loop for performing task and jumping to next page on completion

Macro 1 is performing a task 10 times on a page - I have that covered. Then once the task has been completed I need Macro 2 to jump to the next page (that is also done) and let Macro 1 do its thing again - that's where I have a problem. I'll do this jumping to the next page a predefined number of times, for example 20 times, hence the !LOOP 20. I can't figure out how to programatically do the nesting of the two macros. I know it should be done in JS and I have done both macros in JS - I just don't know how to "tell" that once Macro 1 is done it should execute Macro 2 and jump to the next page and start Macro 1 again.
Here's what I have so far:
var macro1;
macro = "CODE:";
macro += "SET !ERRORIGNORE YES" + "\n";
macro += "VERSION BUILD=000000" + "\n";
macro += "SET !TIMEOUT_STEP 0" + "\n";
macro += "TAG POS={{i}} TYPE=A ATTR=TXT:***" + "\n";
macro += "TAG POS=1 TYPE=SELECT ATTR=TXT:*** CONTENT=***" + "\n";
for(var i=0;i<11;i++)
{
iimDisplay(i);
iimSet("i", i);
iimPlay(macro1);}
var macro2 = "CODE:";
macro += "SET !LOOP 20" + "\n";
macro += "TAG POS=1 TYPE=A ATTR=TXT:Next<SP>›" + "\n";
{
iimPlay(macro2);}
So try this:
var macro1 = "CODE:";
macro1 += "SET !ERRORIGNORE YES" + "\n";
// macro1 += "VERSION BUILD=000000" + "\n";
macro1 += "SET !TIMEOUT_STEP 0" + "\n";
macro1 += "TAG POS={{i}} TYPE=A ATTR=TXT:***" + "\n";
macro1 += "TAG POS=1 TYPE=SELECT ATTR=TXT:*** CONTENT=***" + "\n";
var macro2 = "CODE:";
// macro2 += "SET !LOOP 20" + "\n";
macro2 += "TAG POS=1 TYPE=A ATTR=TXT:Next<SP>›" + "\n";
for (j = 1; j <= 20; j++ ) {
for (i = 1; i <= 10; i++) {
iimDisplay(i);
iimSet("i", i);
iimPlay(macro1);
}
iimPlay(macro2);
}

Imacros saving extracted txt so csv file and adding new line for each word

I have a task to extract txt from website to csv file and save info in 1 line,
here is my code
Problem seems is because i'm using at the end of extract=txt\n, how i can make it in one line and keep same syntax as i'm using.
for(var i = 1; i <= 6; i++) {
iimPlay("CODE:"
+ 'SET !EXTRACT_TEST_POPUP NO\n'
+ 'TAG POS='+i+' TYPE=DIV ATTR=CLASS:"divtd textcenter vam" EXTRACT=TXT\n'
+ 'SAVEAS TYPE=EXTRACT FOLDER=* FILE=trafficgoals.csv\n'
);
}
Try in the following way:
var M = "";
for (i = 1; i <= 6; i++)
M += 'TAG POS=' + i + ' TYPE=DIV ATTR=CLASS:"divtd textcenter vam" EXTRACT=TXT\n';
M += 'SET !EXTRACT EVAL("\'{{!EXTRACT}}\'.replace(/\\\\n/g, \'\');")\n';
M += 'SAVEAS TYPE=EXTRACT FOLDER=* FILE=trafficgoals.csv\n';
iimPlayCode(M);

Parsing sectioned file with augeas

I am trying to create a module for parsing vim files which are sectioned in a specific manner. A sample file:
" My section {
set nocompatible " be iMproved
set encoding=utf-8
" }
" vim: set foldmarker={,} foldlevel=0 foldmethod=marker:
While writing the module, I've got stuck at this point:
module Vimrc =
autoload xfm
let section = del "\" " "\" " . key /[^\n]+/ . del "\n" "\n" . store /.*/ . del "\" " "\" "
let lns = [ section . del "\n" "\n" ] *
let filter = (incl "*.vim")
let xfm = transform lns filter
I'm aware that there are some other mistakes, but it complains about the regex key /[^\n]+/, saying:
/tmp/aug/vimrc.aug:3.36-.48:exception: The key regexp /[^ ]+/ matches
a '/'
I do not understand what the / character has got to do with this.
As the error says, your key regexp matches a slash, which is illegal since / is used as a level separator in the tree.
If your section names can contain slashes, you need to store them as a node value, not label, so instead of:
{ "My section"
{ "set" = "nocompatible" { "#comment" = "be iMproved" } } }
you'll have to do:
{ "section" = "My section"
{ "set" = "nocompatible" { "#comment" = "be iMproved" } } }

SPARQL query with filtering

I like to list all drugs that start with some letter to fill autocomplete text box.Here is the code
public string[] GetCompletionList(string prefixText)
{
string rdfDat = AppDomain.CurrentDomain.BaseDirectory + "DrugRDF.rdf";
List<string> list = new List<string>();
TripleStore store = new TripleStore();
Graph rdf = new Graph();
FileLoader.Load(rdf, rdfDat, new RdfXmlParser());
store.Add(rdf);
string tmp = "^" + prefixText;
string sparqlQuery = "PREFIX mojLek: <http://www.example.org/mojLek#>"
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>"
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
+ "SELECT ?x"
+ "WHERE {?h mojLek:ime ?x ."
+ "FILTER regex(str(?x),"+tmp+") }";
SparqlQueryParser sparqlParser = new SparqlQueryParser();
SparqlQuery query = sparqlParser.ParseFromString(sparqlQuery);
Object results = store.ExecuteQuery(query);
if (results is SparqlResultSet)
{
SparqlResultSet r = results as SparqlResultSet;
foreach (SparqlResult res in r)
{
list.Add(res["x"].ToString().ToLower());
}
}
return list.ToArray();
}
However if I try it with for example A there are already couples that starts with A I got this error
VDS.RDF.Parsing.RdfParseException: [Line 1 Column 263] The value 'A' is not valid as a QName
at VDS.RDF.Parsing.Tokens.SparqlTokeniser.TryGetQNameOrKeyword()
at VDS.RDF.Parsing.Tokens.SparqlTokeniser.GetNextToken()
at VDS.RDF.Parsing.Tokens.TokenQueue.InitialiseBuffer()
at VDS.RDF.Parsing.SparqlQueryParser.ParseInternal(SparqlQueryParserContext context)
at VDS.RDF.Parsing.SparqlQueryParser.ParseInternal(TextReader input)
at VDS.RDF.Parsing.SparqlQueryParser.ParseFromString(String queryString)
at SuggestWebService.GetCompletionList(String prefixText) in d:\Suggest\App_Code\SuggestWebService.cs:line 57
Put newlines in the query string to make the error messages better.
There are no SPARQL quotes at
regex(str(?x),"+tmp+")
Try:
regex(str(?x),'"+tmp+"')
which puts single quotes into the SPARQL. Be careful of any quotes in tmp.
I have changed my code in this way so it worked for me
string tmp="^"+prefixText;
var query = "PREFIX mojLek: <http://www.example.org/mojLek#>"
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>"
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
+ "PREFIX fn: <http://www.w3.org/2005/xpath-functions#>"
+ "SELECT ?x ?h"
+ "WHERE {?h mojLek:ime ?x ."
+ "FILTER regex(?x,\""+tmp+"\")"
+"}";