If statement does not works correctly - javascript

I need some support. I am a newbie and I try to build a function which checks 2 entries in different sheets (both in one spreadsheet) if the condition "state" is true. If so the function has to set a value. If the condition is false, the function has to put a different value.
Currently the function set alway the same value (13:00) instead of the stored values.
I hope an one can help. Code below:
//Create an UI menu
function onOpen() {
var ui = SpreadsheetApp.getUi().createMenu('⏰');
ui.addItem('🔄 Update ETAs','etaCheck')
ui.addToUi();
}
function etaCheck(){
// Defined variabels
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ds = ss.getSheetByName('support ETA');
var ms = ss.getSheetByName('KPI Tracker 22');
var eta = ds.getRange('G2:G').getValues();
var tms1 = ds.getRange('E2:E').getValues();
var tms2 = ms.getRange('O2:O').getValues();
var state = ds.getRange('F2:F').getValues();
// If statement
if(tms1 === tms2 && state === 'Update'){
ms.getRange('R2').setValue(eta)
}else{
ms.getRange('R2').setValue(eta)
}
}

I can't be sure but I think this is closer to what you desire
function etaCheck() {
const ss = SpreadsheetApp.getActive();
const sh1 = ss.getSheetByName('support ETA');
const sh2 = ss.getSheetByName('KPI Tracker 22');
const o = sh2.getRange(2, 18, sh2.getLastRow() - 1).getValues();
const eta = sh1.getRange('G2:G' + sh1.getLastRow()).getValues().flat();
const tms1 = sh1.getRange('E2:E' + sh1.getLastRow()).getValues().flat();
const tms2 = sh2.getRange('O2:O' + sh2.getLastRow()).getValues().flat();
const state = sh1.getRange('F2:F' + sh1.getLastRow()).getValues().flat();
tms1.forEach((e,i) => {
if(e == tms2[i] && state[i] == 'Update') {
o[i][0] = eta[i]
sh2.getRange(i + 2, 18).setValue(eta[i]);
} else {
sh2.getRange(i + 2, 18).setValue(eta[i]);//This does not make much sense since you are doing the same thing in both cases
}
})
}

Related

Faster way to collect data from JSON than looping in spreadsheet

I am learning Javascript and this is my first time working with Google Sheets Apps Script. What I am doing is taking a large JSON file and importing it into my sheet. After that I am populating a few hundred properties based on the key:value found in the JSON.
This is how it kinda works right now:
Go to first column and first row of my sheet.
Get the name (property name).
Search the JSON for the key and then grab the value.
Update a neighbor cell with the value found in the JSON.
Right now it all works the only issue is it seems to be pretty slow. It takes about .5-1 second per lookup and when I have 200+ properties it just seems slow. This might just be a limitation or it might be my logic.
My sheet can be found here: https://docs.google.com/spreadsheets/d/1tt3eh1RjL_CbUIaPzj10DbocgyDC0iNRIba2B4YTGgg/edit#gid=0
My function that does everything:
function parse() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange(2,1);
var range1 = sheet.getRange("A2");
var cell = range.getCell(1, 1);
var event_line = cell.getValue();
var tmp = event_line.split(". ");
var col_number = tmp[0];
var event_name = tmp[1];
event_json = get_json_from_cell(col_number);
const obj = JSON.parse(event_json);
var traits = obj.context.traits;
var properties = obj.properties;
//Get the range for the section where properties are
var traits_range = sheet.getRange("contextTraits");
var allprop = sheet.getRange("testAll");
var alllen = allprop.getNumRows();
var length = traits_range.getNumRows();
for (var i = 1; i < length; i++) {
var cell = traits_range.getCell(i, 1);
var req = traits_range.getCell(i, 4).getValue();
var trait = cell.getValue();
var result = traits[trait];
var result_cell = traits_range.getCell(i, 3);
if (result == undefined) {
if (req == "y"){
result = "MISSING REQ";
result_cell.setBackground("red");
} else {
result = "MISSING";
result_cell.setBackground("green");
}
} else {
result_cell.setBackground("blue");
}
result_cell.setValue(result);
Logger.log(result);
}
for (var i = 1; i < alllen; i++) {
var cell = allprop.getCell(i,1);
var req = allprop.getCell(i, 4).getValue();
var prop = cell.getValue();
var result = properties[prop];
var result_cell = allprop.getCell(i, 3);
if (result == undefined) {
if (req == "y"){
result = "MISSING REQ";
result_cell.setBackground("red");
} else {
result = "MISSING";
result_cell.setBackground("green");
}
} else {
result_cell.setBackground("blue");
}
result_cell.setValue(result);
}
Logger.log(result);
}

setValues from JSON array to spreadsheet

I'm stuck trying to copy the values from my JSON scraping script to spreadsheet.
Does anyone know how to do that?
I'm stuck trying to know how to get the "longname" values to the memory and then using setValue once at the end.
I need to paste all the values here at the column B.
First I'm trying to resolve a single column, later I will need to paste a multi dimensional array to multiple columns. But that's only if I solve this.
Just a detail, on columnWithTickers I used only a range of 5 rows for testing purposes. Later I will use a dynamic value.
Code:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var page1 = ss.getSheetByName("pag1");
let columnWithTickers = page1.getRange(2, 1, 5).getValues();
let targetColumn = page1.getRange(2, 2);
function printValuesFromJSON() {
if (!Array.isArray(columnWithTickers)) {
columnWithTickers = [[columnWithTickers]]
}
return columnWithTickers.map(tickers => {
try {
values = tickers[0].toString().split(",");
let url = `https://finance.yahoo.com/quote/${values}.SA/key-statistics?p=${values}.SA`;
let source = UrlFetchApp.fetch(url).getContentText()
let jsonString = source.match(/root.App.main = ([\s\S\w]+?);\n/)
if (!jsonString || jsonString.length == 1) return;
let data = JSON.parse(jsonString[1].trim());
let longname = data.context.dispatcher.stores.QuoteSummaryStore.price.longName.split();
/*let resultados = longname.map(vetor =>{
return ativosResultados = vetor[0].toString();
})*/
}
catch (error) {
return "N/A"
}
}
)
}
You can try the below function:
function attDiarios(){
var result = intervaloTickers.map(tickersAtuais =>
{
ativosFinais = tickersAtuais[0].toString().split(" ")
let url = `https://finance.yahoo.com/quote/${ativosFinais}.SA/key-statistics?p=${ativosFinais}.SA`;
let source = UrlFetchApp.fetch(url).getContentText()
let jsonString = source.match(/root.App.main = ([\s\S\w]+?);\n/)
if (!jsonString || jsonString.length == 1) return null;
let data = JSON.parse(jsonString[1].trim());
try{
var longNameValue = data.context.dispatcher.stores.QuoteSummaryStore.price.longName;
}
catch(err){
var longNameValue = 'N/A';
}
return [[longNameValue]]
}
)
sImport.getRange(5, 2, result.length, result[0].length).setValues(result);
}

Showing errors, the data validation rule has more items than the limit of 500. Use the list from a range criteria instead

The following codes showing errors due to limit of 500. I tried to solve it and reviewed many docs but failed. Is there any way to solve it?
var spreadsheet = SpreadsheetApp.getActive();
var dashboard = spreadsheet.getSheetByName("Dashboard");
var wsOptions = spreadsheet.getSheetByName("Master");
var options = wsOptions.getRange(2, 1, wsOptions.getLastRow()-1,6).getDisplayValues();
function onEdit(e){
var as = e.source.getActiveSheet();
var val = e.range.getValue();
var val_not = e.range.getA1Notation();
if (val_not =='F5' && as.getName() == "Dashboard"){
if(val === "All"){
dashboard.getRange('G5').setValue(new Date()).setNumberFormat("yyyy-mm-dd");
dashboard.getRange('G5').clearDataValidations();
}
else{
var filteredOptions = options.filter(function(o){return o[5] === val});
var listToApply = filteredOptions.map(function(o){return o[0]}).sort().reverse();
var cell = dashboard.getRange('G5');
var rule = SpreadsheetApp.newDataValidation().requireValueInList(listToApply).setAllowInvalid(false).build();
cell.clearContent();
cell.clearDataValidations();
cell.setDataValidation(rule);
}
}
}
Explanation:
As the error suggests:
The data validation rule has more items than the limit of 500. Use the
‘List from a range’ criteria instead.
you should use requireValueInRange().
In order to use the latter, you need to define a range of data validation items. In the following approach, I create a range of these items opt_range and I use that as an argument for the requireValueInRange() function.
Solution 1:
function onEdit(e){
var as = e.source.getActiveSheet();
var val = e.range.getValue();
var val_not = e.range.getA1Notation();
var spreadsheet = SpreadsheetApp.getActive();
var dashboard = spreadsheet.getSheetByName("Dashboard");
var wsOptions = spreadsheet.getSheetByName("Master");
var options = wsOptions.getRange(2, 1, wsOptions.getLastRow()-1,6).getDisplayValues();
if (val_not =='F5' && as.getName() == "Dashboard"){
if(val === "All"){
dashboard.getRange('G5').setValue(new Date()).setNumberFormat("yyyy-mm-dd");
dashboard.getRange('G5').clearDataValidations();
}
else{
var filteredOptions = options.filter(function(o){return o[5] === val});
var listToApply = filteredOptions.map(function(o){return o[0]}).sort().reverse();
var listToApply2D = listToApply.map(ta=>[ta]);
var jSize = wsOptions.getRange('J:J').getValues().filter(String).length;
if (jSize>0){ wsOptions.getRange(1,10,jSize,1).clearContent()};
wsOptions.getRange(1,10,listToApply2D.length,listToApply2D[0].length).setValues(listToApply2D);
var opt_range = wsOptions.getRange(1,10,listToApply2D.length,listToApply2D[0].length);
var cell = dashboard.getRange('G5');
var rule = SpreadsheetApp.newDataValidation().requireValueInRange(opt_range).build();
cell.clearContent();
cell.clearDataValidations();
cell.setDataValidation(rule);
}
}
}
Solution 2 (recommended):
Assuming you don't have 500 unique items in the list, you can still use the code you posted, but take the unique list of the items:
function onEdit(e){
var spreadsheet = SpreadsheetApp.getActive();
var dashboard = spreadsheet.getSheetByName("Dashboard");
var wsOptions = spreadsheet.getSheetByName("Master");
var options = wsOptions.getRange(2, 1, wsOptions.getLastRow()-1,6).getDisplayValues();
var as = e.source.getActiveSheet();
var val = e.range.getValue();
var val_not = e.range.getA1Notation();
if (val_not =='F5' && as.getName() == "Dashboard"){
if(val === "All"){
dashboard.getRange('G5').setValue(new Date()).setNumberFormat("yyyy-mm-dd");
dashboard.getRange('G5').clearDataValidations();
}
else{
var filteredOptions = options.filter(function(o){return o[5] === val});
var listToApply = filteredOptions.map(function(o){return o[0]}).sort().reverse();
var uniqueList = listToApply.filter((v, i, a) => a.indexOf(v) === i); // <= New code
var cell = dashboard.getRange('G5');
var rule = SpreadsheetApp.newDataValidation().requireValueInList(uniqueList).setAllowInvalid(false).build();
cell.clearContent();
cell.clearDataValidations();
cell.setDataValidation(rule);
}
}
}
Instead of using requireValueInList use requireValueInRange. This implies that you should add the values in the list to a range.
Resources
https://developers.google.com/apps-script/reference/spreadsheet/data-validation-builder#requireValueInRange(Range)
https://developers.google.com/apps-script/reference/spreadsheet/data-validation-builder#requirevalueinrangerange,-showdropdown

How to empty an Array in a Script

I have a script that uses AJAX/PHP/SQL to query data and pushes it into an array with a bunch of IF's statements. The changeData function is called every 6 seconds. The first query I return I have 6 arrays. The second time i send a request, my push array(IsVacant1) is double and went to 12. after a while, I have over 500 arrays going into my .each statement.
How do I 'clear' this every time I make a request so that I am not adding arrays? Any help is most appreciated.
function changeData() {
isPaused = true;
var mydata0 = null;
$.post('php/ProductionChange.php', {
'WC': cc
}, function(data) { // This is Where I use an AJAX call into a php file.
mydata0 = data; // This takes the array from the call and puts it into a variable
var pa = JSON.parse(mydata0); // This parses the data into arrays and elements
var temp = {};
var bayData = '';
if (pa != null) {
for (var i = 0; i <= pa.length - 1; i++) {
var job = pa[i][0];
var shipdate = pa[i][1];
var status = pa[i][2];
var name = pa[i][3];
var EnclLoc = pa[i][13];
var Enclsize = pa[i][14];
var backpan = pa[i][15];
var percentCom = pa[i][16];
var IsVisible = pa[i][17];
var png = pa[i][18];
var WorkC = pa[i][20];
baydata = 'bayData' + i + '';
temp = {
job, shipdate, name, EnclLoc, Enclsize, backpan, percentCom, IsVisible, png, WorkC, status
};
isVacant1.push({
baydata: temp
});
}
} else {
ii = 1;
//alert("There are no more job numbers in this bay location. Thank you. ");
}
$.each(isVacant1, function(key, value) {
var job = value.baydata.job;
var ship = value.baydata.shipdate;
var name = value.baydata.name;
var encl = value.baydata.EnclLoc;
var EnclSize = value.baydata.EnclLoc;
var percentCom = value.baydata.percentCom;
var backpan = value.baydata.backpan;
var PngLogo = value.baydata.png;
var IsVisible = value.baydata.IsVisible;
var WorkC = value.baydata.WorkC;
var status = value.baydata.status;
var p = WorkC;
WorkC = (WorkC < 10) ? ("0" + WorkC) : WorkC;
//// remember if the encl location matches the workcell cell then do stuff based on that....... hint encl image not hiding becase of duplicate 17s
if (((encl == p) || (backpan == p)) && job != 123) {
$('#WC' + p).show();
document.getElementById("bayData" + p).innerHTML = name + ' ' + ship; // Work Cell Name and Ship Date
document.getElementById("bayData" + p + "a").innerHTML = job; // Work cell Job Number
document.getElementById("percentCom" + p).innerHTML = percentCom + '%'; // Work Cell Percent Complete
} else {
$('#WC' + p).hide();
From your question it looks like you want to clear the isVacant1 array.
In your ajax callback just put isVacant1 = []; as the first line. Like this
function(data) { // This is Where I use an AJAX call into a php file.
isVacant1 = [];
mydata0 = data; // This takes the array from the call and puts it into a variable
var pa = JSON.parse(mydata0); // This parses the data into arrays and elements
var temp = {};
var bayData = '';
..................
From your code it's not clear how you are declaring/initializing isVacant1 so i have suggested isVacant1 = [] otherwise you can also use isVacant1.length = 0.
You can also take a look here How do I empty an array in JavaScript?

How to return array from JavaScript function that retrieves data from text file?

I am building a Windows 8 Store app with HTML/CSS/JavaScript. I am reading in data from a text file through a function, and then putting that data into an array. I am trying to return the array through the function, but it is not working. Any help would be greatly appreciated. I've attached my code snippet.
// Load user data
var DefineUserData = function LoadUserData() {
return Windows.Storage.ApplicationData.current.localFolder.getFileAsync(loadfile).done(function (UserFile) {
return Windows.Storage.FileIO.readTextAsync(UserFile).done(function (fileResult) {
var userdata = new Object();
var dataobject = {};
var innercount;
var outercount;
var fileResultByLines = fileResult.split("\n");
for (outercount = 0; outercount <= (fileResultByLines.length - 2) ; outercount++) {
var tempArray = fileResultByLines[outercount].split(",");
dataobject.metrictitle = tempArray[0];
dataobject.numinputs = tempArray[1];
dataobject.inputs = new Array();
for (innercount = 0; innercount <= parseInt(dataobject.numinputs) ; innercount++) {
dataobject.inputs[innercount] = tempArray[innercount + 2];
}
userdata[outercount] = dataobject;
}
return userdata;
});
},
function (errorResult) {
document.getElementById("resbutton1").innerText = errorResult;
})
}
Your DefineUserData function is returning a Promise, not a value. Additionally done functions don't return anything. Instead you'll need to use then functions instead of done functions in DefineUserData and then handle add a done function (or then) to the code that calls this function.
Also, You can make your promises easier to read, and easier to work with by chaining then functions instead of nesting them.
Currently on Win7 at the office so I can't test this, but try something similar to this pseudo-code. Note then functions instead of done. The last then returns your data. Sample snippet afterwards to illustrate calling this and handling the result.
// modified version of yours
var DefineUserData = function LoadUserData() {
return Windows.Storage.ApplicationData.current.localFolder
.getFileAsync(loadfile)
.then(function (UserFile) {
return Windows.Storage.FileIO.readTextAsync(UserFile);
}).then(function (fileResult) {
var userdata = new Object();
var dataobject = {};
var innercount;
var outercount;
var fileResultByLines = fileResult.split("\n");
for (outercount = 0; outercount <= (fileResultByLines.length - 2) ; outercount++) {
var tempArray = fileResultByLines[outercount].split(",");
dataobject.metrictitle = tempArray[0];
dataobject.numinputs = tempArray[1];
dataobject.inputs = new Array();
for (innercount = 0; innercount <= parseInt(dataobject.numinputs) ; innercount++) {
dataobject.inputs[innercount] = tempArray[innercount + 2];
}
userdata[outercount] = dataobject;
}
return userdata;
},
function (errorResult) {
document.getElementById("resbutton1").innerText = errorResult;
});
}
// some other code...
DefineUserData.done(function (userdata) {
// do something
});

Categories

Resources