Issues with google - javascript

Is it possible, depending on the environment of the company, that some features of Google are non-existent? I tried some functions with my personal email and it works; but, in a professional environment it doesn't work.
function getDataForSearch() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ws = ss.getSheetByName("PEP FORMULAR");
return ws.getRange(6, 1, ws.getLastRow()-1, 6).getValues();
}
var data;
function setDataForSearch(){
google.script.run.withSuccessHandler(function(dataReturned){
data = dataReturned.slice();
}).getDataForSearch();
}
function search(){
var searchInput = document.getElementById("searchInput").value.toString().toLowerCase().trim();
var searchWords = searchInput.split(/\s+/);
//var searchColumns = [0,1,2,3,4,5];
var searchColumns = [0];
//and
var resultsArray = searchInput == "" ? [] : data.filter(function(r){
return searchWords.every(function(word){
return searchColumns.some(function(colIndex){
return r[colIndex].toString().toLowerCase().indexOf(word) != -1;
});
});
});
var searchResultsBox = document.getElementById("searchResults");
var templateBox = document.getElementById("rowTemplate");
var template = templateBox.content;
searchResultsBox.innerHTML = "";
resultsArray.forEach(function(r){
var tr = template.cloneNode(true);
var typeInitiativeColumn = tr.querySelector(".type-initiative");
var pepRefColumn = tr.querySelector(".pep-ref");
var projectNameColumn = tr.querySelector(".project-name");
pepRefColumn.textContent = r[0];
typeInitiativeColumn.textContent = r[2];
projectNameColumn.textContent = r[5];
searchResultsBox.appendChild(tr);
});
}
This code works but, when I try to reproduce it in a professional environment, the function setDataForSerach() is not able to get the data from the function getDataForSearch() and make a copy.

Related

Set values from different sheets to another sheet

In summary, I'm trying to simplify this function that load values from two different sheets to another sheet.
All the values are stored in rows in two sheets (DBClienti and DataBkp), all these rows have a reference cell with a unique ID. I select an ID from DBClienti and the function find the relative row number, corresponding to the data to load in the last sheet (Quota).
I'm setting this data using all those vars, but of course there is a better (and right) way that I don't know.
function loadDataBkp() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheetQuota = ss.getSheetByName('Quota');
const sheetDBClienti = ss.getSheetByName("DBClienti");
const sheetDataBkp = ss.getSheetByName("DataBkp");
//Reset Inputs
resetQuota();
//Select the ID DOC
var selectedIDDoc = sheetDBClienti.getActiveCell();
var selectedIDDocVal = selectedIDDoc.getValue();
//Find row of ID DOC in DBClienti
var rowDBClienti;
const dataDBClienti = sheetDBClienti.getDataRange().getValues();
for(var i = 0; i<dataDBClienti.length;i++){
if(dataDBClienti[i][9] == selectedIDDocVal){
rowDBClienti = i+1;
}
}
//Set values in Quota - list
var valI4 = sheetDBClienti.getRange(rowDBClienti,1).getValue();
var valI5 = sheetDBClienti.getRange(rowDBClienti,2).getValue();
var valI6 = sheetDBClienti.getRange(rowDBClienti,3).getValue();
var valI7 = sheetDBClienti.getRange(rowDBClienti,4).getValue();
var valI8 = sheetDBClienti.getRange(rowDBClienti,5).getValue();
var valI9 = sheetDBClienti.getRange(rowDBClienti,6).getValue();
var valI10 = sheetDBClienti.getRange(rowDBClienti,7).getValue();
var valI11 = sheetDBClienti.getRange(rowDBClienti,8).getValue();
sheetQuota.getRange('I4').setValue(valI4);
sheetQuota.getRange('I5').setValue(valI5);
sheetQuota.getRange('I6').setValue(valI6);
sheetQuota.getRange('I7').setValue(valI7);
sheetQuota.getRange('I8').setValue(valI8);
sheetQuota.getRange('I9').setValue(valI9);
sheetQuota.getRange('I10').setValue(valI10);
sheetQuota.getRange('I11').setValue(valI11);
//Find row of ID DOC in DataBkp
var rowDataBkp;
const dataDataBkp = sheetDataBkp.getDataRange().getValues();
for(var i = 0; i<dataDataBkp.length;i++){
if(dataDataBkp[i][0] == selectedIDDocVal){
rowDataBkp = i+1;
}
}
//Set values in Quota - sections
var valC2 = sheetDataBkp.getRange(rowDataBkp,2).getValue();
var valC4 = sheetDataBkp.getRange(rowDataBkp,3).getValue();
var valC5 = sheetDataBkp.getRange(rowDataBkp,4).getValue();
var valC6 = sheetDataBkp.getRange(rowDataBkp,5).getValue();
var valC7 = sheetDataBkp.getRange(rowDataBkp,6).getValue();
var valC8 = sheetDataBkp.getRange(rowDataBkp,7).getValue();
var valC9 = sheetDataBkp.getRange(rowDataBkp,8).getValue();
var valC10 = sheetDataBkp.getRange(rowDataBkp,9).getValue();
var valC11 = sheetDataBkp.getRange(rowDataBkp,10).getValue();
var valC12 = sheetDataBkp.getRange(rowDataBkp,11).getValue();
var valF4 = sheetDataBkp.getRange(rowDataBkp,12).getValue();
var valF5 = sheetDataBkp.getRange(rowDataBkp,13).getValue();
var valF8 = sheetDataBkp.getRange(rowDataBkp,14).getValue();
var valF9 = sheetDataBkp.getRange(rowDataBkp,15).getValue();
var valF12 = sheetDataBkp.getRange(rowDataBkp,16).getValue();
var valF13 = sheetDataBkp.getRange(rowDataBkp,17).getValue();
var valF25 = sheetDataBkp.getRange(rowDataBkp,18).getValue();
var valF26 = sheetDataBkp.getRange(rowDataBkp,19).getValue();
var valF27 = sheetDataBkp.getRange(rowDataBkp,20).getValue();
var valI14 = sheetDataBkp.getRange(rowDataBkp,21).getValue();
sheetQuota.getRange('C2').setValue(valC2);
sheetQuota.getRange('C4').setValue(valC4);
sheetQuota.getRange('C5').setValue(valC5);
sheetQuota.getRange('C6').setValue(valC6);
sheetQuota.getRange('C7').setValue(valC7);
sheetQuota.getRange('C8').setValue(valC8);
sheetQuota.getRange('C9').setValue(valC9);
sheetQuota.getRange('C10').setValue(valC10);
sheetQuota.getRange('C11').setValue(valC11);
sheetQuota.getRange('C12').setValue(valC12);
sheetQuota.getRange('F4').setValue(valF4);
sheetQuota.getRange('F5').setValue(valF5);
sheetQuota.getRange('F8').setValue(valF8);
sheetQuota.getRange('F9').setValue(valF9);
sheetQuota.getRange('F12').setValue(valF12);
sheetQuota.getRange('F13').setValue(valF13);
sheetQuota.getRange('F25').setValue(valF25);
sheetQuota.getRange('F26').setValue(valF26);
sheetQuota.getRange('F27').setValue(valF27);
sheetQuota.getRange('I14').setValue(valI14);
}
Try it this way:
function loadDataBkp() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sh1 = ss.getSheetByName('Quota');
const sh2 = ss.getSheetByName("DBClienti");
const sh3 = ss.getSheetByName("DataBkp");
resetQuota();
var v2 = sh2.getActiveCell().getValue();
var row1;
sh2.getDataRange().getValues().forEach((r,i) => { if (r[9] == v2) { row1 = i + 1; } })
sh1.getRange(4, 9, 8).setValues(sh2.getRange(row1, 1, 1, 8).getValues().flat().map(v => [v]))
var row2;
const vs3 = sh3.getDataRange().getValues().forEach((r, i) => { if (r[0] == v2) { row2 = i + 1 } })
let xvs = sh3.getRange(row2, 2, 1, 20).getValues().flat();
['C2', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9', 'C10', 'C11', 'C12', 'F4', 'F5', 'F8', 'F9', 'F12', 'F13', 'F25', 'F26', 'F27', 'I14'].forEach((s, i) => { sh1.getRange(s).setValue(xvs[i]) });
}

Function call within a function not working

I have a function I call to initialize a page :
function initialize_selections() {
var freeamountElement = document.getElementById("text-freeamount");
var dlyElement = document.getElementById("text-dly");
var freeshipperElement = document.getElementById("text-freeshipper");
if(freeamountElement){
var shippingMethod = document.getElementById("text-freeamount").innerText;
document.getElementById('radio-freeamount').checked = true;
document.getElementById('value-shipping').innerHTML = '$0.00';
document.getElementById('text-shipping').innerHTML = shippingMethod;
}else if(dlyElement){
var shippingMethod = document.getElementById("text-dly").innerText;
document.getElementById('radio-dly').checked = true;
document.getElementById('value-shipping').innerHTML = '$0.00';
document.getElementById('text-shipping').innerHTML = shippingMethod;
}else if(freeshipperElement){
var shippingMethod = document.getElementById("text-freeshipper").innerText;
document.getElementById('radio-freeshipper').checked = true;
document.getElementById('value-shipping').innerHTML = '$0.00';
document.getElementById('text-shipping').innerHTML = shippingMethod;
}else{
var shippingMethod = document.getElementById("text-upsxml").innerText;
var shippingValue = document.getElementById("value-upsxml").innerText;
document.getElementById('radio-upsxml').checked = true;
document.getElementById('value-shipping').innerHTML = shippingValue;
document.getElementById('text-shipping').innerHTML = shippingMethod;
}
// myPoints();
};
This is working well on initial page load. Depending on specfic conditions I want to call it again, from a separate function:
function hideForm(){
if (document.getElementById('thebox').checked==true){
document.getElementById('formdiv').style.display="block";
document.getElementById('formdiv2').style.display="none";
document.getElementById('formdiv3').style.display="block";
var shipping_method = 'Pick Up Your Order';
document.getElementById('text-shipping').innerHTML = shipping_method ;
document.getElementById('value-shipping').innerHTML = '$0.00';
document.querySelector('input[name="shipping"]:checked').checked = false;
// myPoints();
} else {
document.getElementById('formdiv').style.display="none";
document.getElementById('formdiv2').style.display="block";
document.getElementById('formdiv3').style.display="none";
var shipping_method = document.getElementById('text-upsxml').innerText;
document.getElementById('text-shipping').innerHTML = shipping_method ;
var shipping_value = document.getElementById('value-upsxml').innerText;
document.getElementById('value-shipping').innerHTML = shipping_value;
document.querySelector('input[name="shipping"]:checked').checked = false;
document.querySelector('input[name="payment"]:checked').checked = false;
initialize_selections();
};
};
Both codes work well and do as I am looking for except that I can not call initialize_selections from hideForm (same thing occurs with the myPoints function, but commented it out to isolate the issue.

Uncaught TypeError: Cannot read property 'filter' of undefined in crud script

I don't know where my issue is on this. Any assistance is greatly appreciated.
function search(){
var searchInput = document.getElementById("searchInput").value;
var resultsArray = data.filter(function(r){
return r[1].indexOf(searchInput) !== -1;
});
var searchResultsBox = document.getElementById("searchResults");
var templateBox = document.getElementById("rowTemplate");
var template = templateBox.content;
searchResultsBox.innerHTML = "";
resultsArray.forEach(function(r){
var tr = template.cloneNode(true);
var eeIdColumn = tr.querySelector(".nameId");
var firstNameColumn = tr.querySelector(".firstName");
var lastNameColumn = tr.querySelector(".lastName");
eeIdColumn.textContent = r[0];
firstNameColumn.textContent = r[1];
lastNameColumn.textContent = r[2];
searchResultsBox.appendChild(tr);
});

Loop performance converting XML to JSON in JavaScript

I am encountering a performance issue trying to fill in a grid with some data from a webservice.
Thanks to the Timeline analyzer from Chrome, I have found the function wich is causing my performance issue. It's due to a loop to get all the data from an xml and converting it into a JSON.
I've tried different loop (each, for, while) expecting improvement but didn't find better than each.
Here is an example of the xml with one child (but in my case there can be 10000 childs):
<DockDistrSearchOutput>
<groupRecord>
<uniqueIdentifier><![CDATA[KP789281]]></uniqueIdentifier>
<supplierCode><![CDATA[10003]]></supplierCode>
<supplierDescription><![CDATA[GROSFILLEX]]></supplierDescription>
<supplierCommercialContractCode><![CDATA[CTCOM01]]></supplierCommercialContractCode>
<supplierCommercialContractDescription><![CDATA[STANDARD EN EURO]]></supplierCommercialContractDescription>
<supplierAddressChainCode><![CDATA[1]]></supplierAddressChainCode>
<supplierAddressChainDescription><![CDATA[FIL1]]></supplierAddressChainDescription>
<articleCode><![CDATA[13A42001]]></articleCode>
<articleDescription><![CDATA[Banane]]></articleDescription>
<logisticVariantCode><![CDATA[2]]></logisticVariantCode>
<logisticVariantDescription><![CDATA[VL0 BANANE C G100]]></logisticVariantDescription>
<weightFactor><![CDATA[0]]></weightFactor>
<purchasePricePerKilo><![CDATA[13]]></purchasePricePerKilo>
<amount><![CDATA[0]]></amount>
<startDate><![CDATA[2016-07-29]]></startDate>
<endDate><![CDATA[2016-07-31]]></endDate>
<crossDockDiscount />
<crossDockMargin />
<extraMargin><![CDATA[3]]></extraMargin>
<listPrice />
<costPriceRD />
<oldSupplierCode><![CDATA[10003]]></oldSupplierCode>
<oldSupplierCommercialContractCode><![CDATA[CTCOM01]]></oldSupplierCommercialContractCode>
<oldSupplierAddressChainCode><![CDATA[1]]></oldSupplierAddressChainCode>
<oldArticleCode><![CDATA[13A42001]]></oldArticleCode>
<oldLogisticVariantCode><![CDATA[2]]></oldLogisticVariantCode>
<oldStartDate><![CDATA[2016-07-29]]></oldStartDate>
<oldEndDate><![CDATA[2016-07-31]]></oldEndDate>
</groupRecord>
</DockDistrSearchOutput>
And here is the loop used to get the data (15900ms for my test case) :
function getDataFromXml(xml){
var data = [];
$(xml).children().each( function (index) {
uniqueNumber++;
var recStatus = ORIGINAL;
if(this.getAttribute("recordState")!=null){
recStatus = this.getAttribute("recordState");
}
this.setAttribute("recordState", recStatus);
this.setAttribute("lineNumber", uniqueNumber);
var record = [];
if(this.getAttribute("checkedLine") == "true"){
record["checkedLine"] = true;
}else
record["checkedLine"] = false;
// Build the array by reading XML Tagname/value
record["id"] = "ID"+uniqueNumber;
$(this).children().each(function () {
record[this.tagName] = this.text;
});
record["recordState"] = recStatus;
record["rowXML"] = this;
data.push(record);
record = null;
recStatus = null;
});
return data;
}
Here are other try to get the data faster:
function getDataFromXml(xml){
var data = [];
$(xml).children().each( function (index) {
uniqueNumber++;
var recStatus = ORIGINAL;
if(this.getAttribute("recordState")!=null){
recStatus = this.getAttribute("recordState");
}
this.setAttribute("recordState", recStatus);
this.setAttribute("lineNumber", uniqueNumber);
var record = [];
/* First try
var a = $(this).children();
var l = a.length;
while(l--){
record[a[l].tagName] = a[l].text;
}*/
//Second try
var tmp = "{", cur = null;
for(var i =0, ln=this.childElementCount; i<ln;++i)
{
cur = this.children[i];
tmp += '"'+cur.tagName+'":"'+cur.text.replace('"', '')+'",';
}
tmp +='}';
tmp = tmp.replace(',}', '}');
record=JSON.parse(tmp);
if(this.getAttribute("checkedLine") == "true"){
record["checkedLine"] = true;
}else
record["checkedLine"] = false;
// Build the array by reading XML Tagname/value
record["id"] = "ID"+uniqueNumber;
record["recordState"] = recStatus;
record["rowXML"] = this;
data.push(record);
tmp = null;
cur = null;
record = null;
recStatus = null;
});
return data;
}
A try with only JavaScript (9600ms in my test case):
function getDataFromXml(xml){
var data = [];
var a = xml.getElementsByTagName("groupRecord");
var l = a.length;
while(l--){
uniqueNumber++;
var recStatus = ORIGINAL;
var groupRec = a[l];
if(groupRec.getAttribute("recordState")!=null){
recStatus = groupRec.getAttribute("recordState");
}
groupRec.setAttribute("recordState", recStatus);
groupRec.setAttribute("lineNumber", uniqueNumber);
var record = [];
var b = groupRec.childNodes;
var j = b.length;
while(j--){
record[b[j].tagName] = b[j].text;
}
if(groupRec.getAttribute("checkedLine") == "true"){
record["checkedLine"] = true;
}else
record["checkedLine"] = false;
// Build the array by reading XML Tagname/value
record["id"] = "ID"+uniqueNumber;
record["recordState"] = recStatus;
record["rowXML"] = groupRec;
data.push(record);
tmp = null;
cur = null;
record = null;
recStatus = null;
groupRec = null;
}
return data;
}
If someone has any advice to increase the performance of my loop?
Thanks in advance!
Edit: Thanks to Jaromanda X I have improved the performance by using only JavaScript.

Reading and writing values from form doesn't work

I have a code sample where I want to read some data from input fields and write that data to a different div.
In first section the code works properly, but in 2nd section the value is not shown. I used the alert function to check that wether value is received or not. The value is received but not shown.
What is wrong with my code? (below)
function preview() {
$(".preview_block").fadeIn(1000);
$("body").css("overflow","hidden");
$(".preview_block").css("overflow","auto");
/*======================Value fetch for personal details=======================*/
var fastname1 = document.forms["myform"]["fastname"].value;
if(fastname1==""){fastname1="---null---"}
var lastname1 = document.forms["myform"]["lastname"].value;
if(lastname1==""){lastname1="---null---"}
var sex1 = document.forms["myform"]["radio"].value;
if(sex1==""){sex1="---null---"}
var clgid1 = document.forms["myform"]["clgid"].value;
if(clgid1==""){clgid1="---null---"}
var dob1 = document.forms["myform"]["dob"].value;
if(dob1==""){dob1="---null---"}
var fname1 = document.forms["myform"]["fname"].value;
if(fname1==""){fname1="---null---"}
var mname1 = document.forms["myform"]["mname"].value;
if(mname1==""){mname1="---null---"}
var gname1 = document.forms["myform"]["gname"].value;
if(gname1==""){gname1="---null---"}
var mobno1 = document.forms["myform"]["mobno"].value;
if(mobno1==""){mobno1="---null---"}
var pmobno1 = document.forms["myform"]["pmobno"].value;
if(pmobno1==""){pmobno1="---null---"}
var mail1 = document.forms["myform"]["mail"].value;
if(mail1==""){mail1="---null---"}
var addr11 = document.forms["myform"]["addr1"].value;
if(addr11==""){addr11="---null---"}
var addr21 = document.forms["myform"]["addr2"].value;
if(addr21==""){addr21="---null---"}
var city1 = document.forms["myform"]["city"].value;
if(city1==""){city1="---null---"}
var state1 = document.forms["myform"]["state"].value;
if(state1==""){state1="---null---"}
var pcode1 = document.forms["myform"]["pcode"].value;
if(pcode1==""){pcode1="---null---"}
var ps1 = document.forms["myform"]["ps"].value;
if(ps1==""){ps1="---null---"}
var po1 = document.forms["myform"]["po"].value;
if(po1==""){po1="---null---"}
var country1 = document.forms["myform"]["country"].value;
if(country1==""){country1="---null---"}
document.getElementById("p1").innerHTML = fastname1;
document.getElementById("p2").innerHTML = lastname1;
document.getElementById("p3").innerHTML = sex1;
document.getElementById("p4").innerHTML = clgid1;
document.getElementById("p5").innerHTML = dob1;
document.getElementById("p6").innerHTML = fname1;
document.getElementById("p7").innerHTML = mname1;
document.getElementById("p8").innerHTML = gname1;
document.getElementById("p9").innerHTML = mobno1;
document.getElementById("p10").innerHTML = pmobno1;
document.getElementById("p11").innerHTML = mail1;
document.getElementById("p12").innerHTML = addr11;
document.getElementById("p13").innerHTML = addr21;
document.getElementById("p14").innerHTML = city1;
document.getElementById("p15").innerHTML = state1;
document.getElementById("p16").innerHTML = pcode1;
document.getElementById("p17").innerHTML = ps1;
document.getElementById("p18").innerHTML = po1;
document.getElementById("p19").innerHTML = country1;
/*======================Value fetch for XII standered details=======================*/
var qualification1 = $('#qualification option:selected').html();
if(qualification1==""){qualification1="---null---"}
var q1_board1 = $('#q1_board option:selected').html();
if(q1_board1==""){q1_board1="---null---"}
var clg_nm1 = document.forms["myform"]["clg_nm"].value;
if(session1==""){session1="---null---"}
var regno1 = document.forms["myform"]["regno"].value;
if(regno1==""){regno1="---null---"}
var yr_reg1 = document.forms["myform"]["yr_reg"].value;
if(yr_reg1==""){yr_reg1="---null---"}
var rollno1 = document.forms["myform"]["rollno"].value;
if(rollno1==""){rollno1="---null---"}
document.getElementById("q1").innerHTML = qualification1;
document.getElementById("q2").innerHTML = q1_board1;
document.getElementById("q3").innerHTML = clg_nm1;
document.getElementById("q4").innerHTML = regno1;
document.getElementById("q5").innerHTML = yr_reg1;
document.getElementById("q6").innerHTML = rollno1;
}

Categories