map box js; hide and show label - javascript

I've never worked with JavaScript before. I'm trying to create a map where different layers can be hidden per users preference. I came across the helpful example on map box showing the exact code at https://www.mapbox.com/mapbox-gl-js/example/toggle-layers/.
Now, my problem is, because I have numerous point layers of different magnitudes, I was forced to create several layers and filter them based on the desired attribute so each points appearance reflects that specific magnitude. However, I want all these points to be organized into years, so I grouped the layers. So all my layer names look something like this (2006_mag2,2006_mag8,2010_mag3...).
However, I want the hide/show option to show layers based on years. So I was thinking I could do some sort of operator like we use in sql (i.e. '2006%' or a LIKE operator). Looking at some posts a lot of people use '*' in JavaScript?
So this is what it would look like for each layer individually before:
var toggleableLayerIds = [ '2006_mag2', '2010_mag3' ];
for (var i = 0; i < toggleableLayerIds.length; i++) {
var id = toggleableLayerIds[i];
}
and this is my botched attempt at trying to group a number of the layers together:
var toggleableLayerIds = [ '2006.*', '2008.*' ];
for (var i = 0; i < toggleableLayerIds.length; i++) {
var id = toggleableLayerIds[i];
}
Any guidance you guys can provide will be greatly appreciated.

You could try to use loops with regex to group layerIds by date. Sorry I changed the name of your variables. This will get you an object with layerIds grouped by date.
var layerIds = [ '2006_mag2', '2010_mag3', '2006_mag8', '2006_mag1', '2008_mag2'];
var dates = ['2006', '2010'];
var groupedLayers = {};
//init your object to have a member for each date
//this could be done inside the loops below by testing if the array exists before pushing the matching layerId
for (var i=0; i < dates.length; i++) {
groupedLayers[dates[i]] = [];
}
//loop over your layerIds to match with the RegExp
for (var i=0; i < layerIds .length; i++) {
for (var j=0; j < dates.length; j++) {
var searchPattern = new RegExp('^' + dates[j]);
if (searchPattern.test(layerIds[i])) {
groupedLayers[dates[j]].push(layerIds[i]);
}
}
}
console.log(groupedLayers)
Please tell me what exact result you need so I could help you more.

Related

Method push is not adding a value to a vector that previously had the same value

The goal of the code I'm going to present is to create a aux vector that will contain petri nets transitions, arcs and places. I'm dividing a petri net into several groups, each group is a transition with respective input arcs and places.
The issue is the following: After I put the info in the first position of the aux vector, I'm unable to put a place with the same id of the place of the previous group. For example, if I have a transition with place_id=1 and place_id=2, and the next transition have place_id=2 and place_id=3, the code doesn't write the value place_i=2 in the vector for the second group.
function conflict() {
var id = [];
var source = [];
var target = [];
var aux = [];
var cont = [];
var places = pnml.getElementsByTagName("place");
var arcs = pnml.getElementsByTagName("arc");
var transitions = pnml.getElementsByTagName("transition");
for (var i = 0; i < transitions.length; i++) {
target.push(transitions[i].getAttribute("id"));
aux.push([]);
for (var j = 0; j < arcs.length; j++) {
if (arcs[j].getAttribute("target") == transitions[i].getAttribute("id")) {
id.push(arcs[j].getAttribute("id"));
source.push(arcs[j].getAttribute("source"));
//console.log(arcs[j].getAttribute( "source" ));
}
}
//console.log(id);
//console.log(arcs);
//console.log(places);
aux[i].push(id, source, target);
//id.length=0;
target = [];
source = [];
id = [];
}
}
Image of the platform with console open
Thanks in advance
Without knowing a whole lot for the issue, try to change this
aux.push([]);
to this
aux[i]=[];
So that you initialize and fill using an index instead of a push and an index later, for consistency.
Let me know if it helps
EDIT:
Also this
aux[i].push(id, source, target);
to this (maybe? )
aux[i].push({ id: id, source:source, target:target});
You probably want to keep objects in aux, so you need to push an object, not 3 parameters like that

Google apps script select specific rows with specific columns

I am trying to figure out how to pull only specific rows and columns using Google apps script. The =QUERY is not an option to use. Need to keep all of the logic in the script - my actual data set is quite large. To illustrate what I'm trying to solve. I have a little table of test data. TestData From which I only want columns 2,3,5 (zero based index) and only the rows with "fur".
function testFour(sheetID, fromTabName, toTabName) {
var sourceTab = SpreadsheetApp.openById(sheetID).getSheetByName(fromTabName);
var values = sourceTab.getDataRange().getValues();
var columns = [2,3,5]; //only want these columns
var output = new Array();
for (var i = 0; i < values.length; i++) {
for (var j = 0; j < values[0].length; j++) {
if (values[i][4] == "fur") { // only want these rows
if (j in columns) {
output.push(values[i][j]);
}
}
}
}
var targetTab = SpreadsheetApp.openById(sheetID).getSheetByName(toTabName);
targetTab.getRange(1, 1, output.length, columns.length).setValues(output);
}
This code bit works but it is not very maintainable if there are many columns:
for(n=0; n < values.length; ++n) {
var columns = [2,3,5];
if (values[n][4] === 'fur') {
output.push( [values[n][2], values[n][3], values[n][5]]);
}
When I run the testFour function I get an error about "Cannot covert Array to Object[][] on this line of the code:
targetTab.getRange(1, 1, output.length, columns.length).setValues(output);
Appreciate any help. I was really surprised to not have found any examples of this. Every solution was using the =QUERY on the sheet.
Based on Cooper's suggestion above this was how I was able to get this to work:
function testFourNew(sheetID, fromTabName, toTabName) {
var sourceTab = SpreadsheetApp.openById(sheetID).getSheetByName(fromTabName);
var targetTab = SpreadsheetApp.openById(sheetID).getSheetByName(toTabName);
var srcrg = sourceTab.getDataRange();
var srcvA = srcrg.getValues();
var desvA=[];
// var columns = [2,3,5]; //only want these columns
var columns = String("2,3,5").split(','); //only want these columns
var tstVal = "fur";
var tstCol = 4;
for (var i=0;i<srcvA.length;i++) {
var tA=[];
if (srcvA[i][tstCol] ==tstVal) {
for (var j=0;j<columns.length;j++) {
//Logger.log(srcvA[i][columns[j]]);
tA.push(srcvA[i][columns[j]]);
}
desvA.push(tA);
}
}
targetTab.getRange(1, 1, desvA.length, desvA[0].length).setValues(desvA);
}
Thank you Cooper your direction and suggestions!
EDIT:
Don't need [] around output if using this line to push to output -
output.push( [values[n][2], values[n][3], values[n][5]]);
To set values by row, data has to be in this format -
[ row,
row ]
or,
[ [1,2,3],
[4,5,6] ]
Cannot covert Array to Object[][] error is shown when range and value format do not match.
Try this line -
targetTab.getRange(1, 1, output.length, columns.length).setValues([output]);
Report Builder
I'm guessing that you want to use this function to build different reports from the same dataset. So I might try something like this:
The Function:
function testFour(t4Obj) {
var ss=SpreadsheetApp.openById(t4Obj.ssId);
var srcsh=ss.getSheetByName(t4Obj.srcShName);
var dessh=ss.getSheetByName(t4Obj.desShName);
var colA=String(t4Obj.zbcols).split('~~~');
var tstCol=Number(t4Obj.zbtstCol);
var tstVal=t4Obj.tstVal;
var srcrg=srchsh.getDataRange();
var srcvA=srcrg.getValues();
var desvA=[];
for (var i=0;i<srcvA.length;i++) {
var tA=[];
if (srcVa[i][tstCol]==tstVal) {
for (var j=0;j<colA.length;j++) {
tA.push(colA[j]);
}
desvA.push(tA);
}
}
dessh.getRange(1, 1, desvA.length, desvA[0].length).setValues(desvA);
}
The t4Obj Data Table:
Of course you would have to build yourself a function that loads tthe t4Obj from the above table. From looking at your code, I'd guess that will be no problem for you.
And please note I have not tested any of this code so it is extremely unlikely to work the first time out of the box. You can sharpen your debugging skills on it.
Let's try making a array for a square range where the number incremented in by one in the same column position down each row. The array would look like this:
var A=[[1,6,11,16,21],[2,7,12,17,22],[3,8,13,18,23],[4,9,14,19,24][5,10,15,20,25]];
Make an Array by hand
function makeArray() {
var A=[[1,6,11,16,21],[2,7,12,17,22],[3,8,13,18,23],[4,9,14,19,24],[5,10,15,20,25]];
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
var rg=sh.getRange(5,5,5,5);//could be this way
//var rg=sh.getRange(5,5,A.length,A[0].length);//or in most programs this way
rg.setValues(A);
}

How can I dynamically index through datalayer tags in GTM?

I'm using the DuracellTomi datalayer plugin to push cart data from woocommerce to a GTM model to handle some tracking.
The DuracellTomi plugin pushes content to the transactionProducts[] array in the following format:
transactionProducts: Array[1]
0 : Object
category:""
currency:"USD"
id:8
name:"Test"
price:100
quantity:"1"
sku:8
I'd like to loop through this array and unstack it into three separate arrays, pricelist, skulist, and quantitylist. Currently I anticipate doing so as some variation on
//Get Product Information
if(stack = {{transactionProducts}}){
for(i = 0; i < stack.length; i++) {
if(stack.i.sku){
skulisttemp.i = stack.i.sku;
}
if(stack.i.price){
pricelisttemp.i = stack.i.price;
}
if(stack.i.sku){
quantitylisttemp.i = stack.i.quantity;
}
}
{{skulist}} = skulisttemp;
{{pricelist}} = pricelisttemp;
{{quantitylist}} = quantitylisttemp;
}
Obviously this is not going to work because of how the tag referencing is set up, but I'm wondering if anyone has dealt with this and knows what the best way to index through these arrays might be. (For those who don't know, the square bracket array call doesn't work with GTM variables and instead the . format is used instead.)
You would need to create 3 variable type custom javascript function that picks your required value from dataLayer and returns it in an array.
Something like
function(){
var products = {{transactionProducts}};
var skuArray = [];
for(i = 0; i < products.length; i++) {
if(products[i].sku){
skuArray.push(products[i].sku)
}
}
return skuArray
}
hope this helped you :)

How can I change multiple ranges of cells background color with a script?

First I am fairly new to JS. I have learned a bunch of it back in college and now I am brushing the dust off to get back at it.
In a previous formula I was able to clear multiple ranges of cells back to nothing with the push of a button. I now want to take a derivative of that formula and change the cells that are within the aforementioned ranges to a different background color.
Here is the "clear" formula:
function clear1() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Main Board');
var rangesToClear = ['D25:I58','D62:I114','D118:I123','D127:I142','D146:I157','D161:I173','D179:I237','D241:I253','D257:I272','D276:I289','D295:I302','D306:I328','D332:I345','D349:I356','D362:I371','D375:I397','D401:I402','D409:I424','D428:I428','D434:I448','D452:I471','D475:I514','D518:I536','D540:I582','D586:I593','D597:I605','D611:I628','D632:I641','D645:I653','D657:I666','D672:I681','D685:I707','D711:I721','D727:I752','D756:I762','D766:I772','D778:I793','D797:I805','D809:I817','D823:I840','D844:I851','D857:I908','D912:I912','D916:I944','D948:I956','D960:I981','D985:I1004','D1010:I1031','D1035:I1051','D1056:I1064','D1069:I1079','D1083:I1094','D1098:I1110'];
for (var i=0; i<rangesToClear.length; i++) {
sheet.getRange(rangesToClear[i]).clearContent();
}
}
I do not completely understand this formula as I was helped to make it. Here is my newer rendition to change the background of the cells. What I did was pare back the ranges in which I want the variables to hit.
function colorup() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Main Board');
var rangesMon = ['D25:D58','D62:D114','D118:D123','D127:D142','D146:D157','D161:D173','D179:D237','D241:D253','D257:D272','D276:D289','D295:D302','D306:D328','D332:D345','D349:D356','D362:D371','D375:D397','D401:D402','D409:D424','D428:D428','D434:D448','D452:D471','D475:D514','D518:D536','D540:D582','D586:D593','D597:D605','D611:D628','D632:D641','D645:D653','D657:D666','D672:D681','D685:D707','D711:D721','D727:D752','D756:D762','D766:D772','D778:D793','D797:D805','D809:D817','D823:D840','D844:D851','D857:D908','D912:D912','D916:D944','D948:I956','D960:D981','D985:D1004','D1010:D1031','D1035:D1051','D1056:D1064','D1069:D1079','D1083:D1094','D1098:D1110'];
var rangesTues = ['E25:E58','E62:E114','E118:E123','E127:E142','E146:E157','E161:E173','E179:E237','E241:E253','E257:E272','E276:E289','E295:E302','E306:E328','E332:E345','E349:E356','E362:E371','E375:E397','E401:E402','E409:E424','E428:E428','E434:E448','E452:E471','E475:E514','E518:E536','E540:E582','E586:E593','E597:E605','E611:E628','E632:E641','E645:E653','E657:E666','E672:E681','E685:E707','E711:E721','E727:E752','E756:E762','E766:E772','E778:E793','E797:E805','E809:E817','E823:E840','E844:E851','E857:E908','E912:E912','E916:E944','E948:E956','E960:E981','E985:E1004','E1010:E1031','E1035:E1051','E1056:E1064','E1069:E1079','E1083:E1094','E1098:E1110']
var rangesWed =['F25:F58','F62:F114','F118:F123','F127:F142','F146:F157','F161:F173','F179:F237','F241:F253','F257:F272','F276:F289','F295:F302','F306:F328','F332:F345','F349:F356','F362:F371','F375:F397','F401:F402','F409:F424','F428:F428','F434:F448','F452:F471','F475:F514','F518:F536','F540:F582','F586:F593','F597:F605','F611:F628','F632:F641','F645:F653','F657:F666','F672:F681','F685:F707','F711:F721','F727:F752','F756:F762','F766:F772','F778:F793','F797:F805','F809:F817','F823:F840','F844:F851','F857:F908','F912:F912','F916:F944','F948:F956','F960:F981','F985:F1004','F1010:F1031','F1035:F1051','F1056:F1064','F1069:F1079','F1083:F1094','F1098:F1110']
var rangesThurs =['G25:G58','G62:G114','G118:G123','G127:G142','G146:G157','G161:G173','G179:G237','G241:G253','G257:G272','G276:G289','G295:G302','G306:G328','G332:G345','G349:G356','G362:G371','G375:G397','G401:G402','G409:G424','G428:G428','G434:G448','G452:G471','G475:G514','G518:G536','G540:G582','G586:G593','G597:G605','G611:G628','G632:G641','G645:G653','G657:G666','G672:G681','G685:G707','G711:G721','G727:G752','G756:G762','G766:G772','G778:G793','G797:G805','G809:G817','G823:G840','G844:G851','G857:G908','G912:G912','G916:G944','G948:G956','G960:G981','G985:G1004','G1010:G1031','G1035:G1051','G1056:G1064','G1069:G1079','G1083:G1094','G1098:G1110']
var rangesFri = ['H25:H58','H62:H114','H118:H123','H127:H142','H146:H157','H161:H173','H179:H237','H241:H253','H257:H272','H276:H289','H295:H302','H306:H328','H332:H345','H349:H356','H362:H371','H375:H397','H401:H402','H409:H424','H428:H428','H434:H448','H452:H471','H475:H514','H518:H536','H540:H582','H586:H593','H597:H605','H611:H628','H632:H641','H645:H653','H657:H666','H672:H681','H685:H707','H711:H721','H727:H752','H76:H762','H766:H772','H778:H793','H797:H805','H809:H817','H823:H840','H844:H851','H857:H908','H912:H912','H916:H944','H948:H956','H960:H981','H985:H1004','H1010:H1031','H1035:H1051','H1056:H1064','H1069:H1079','H1083:H1094','H1098:H1110']
var rangesSat = ['I25:I58','I62:I114','I118:I123','I127:I142','I146:I157','I161:I173','I179:I237','I241:I253','I257:I272','I276:I289','I295:I302','I306:I328','I332:I345','I349:I356','I362:I371','I375:I397','I401:I402','I409:I424','I428:I428','I434:I448','I452:I471','I475:I514','I518:I536','I540:I582','I586:I593','I597:I605','I611:I628','I632:I641','I645:I653','I657:I666','I672:I681','I685:I707','I711:I721','I727:I752','I756:I762','I766:I772','I778:I793','I797:I805','I809:I817','I823:I840','I844:I851','I857:I908','I912:I912','I916:I944','I948:I956','I960:I981','I985:I1004','I1010:I1031','I1035:I1051','I1056:I1064','I1069:I1079','I1083:I1094','I1098:I1110']
for (var i=0; i<rangesMon.length; i++){
sheet.getRange(rangesMon[i]).setBackground(color)
}
As you can see I added more vars, one for each day of the week since each day needs to be a different color. Towards the end I fall short as I am unsure of exactly what needs to be added to make the color change. One other thing that I am having trouble finding is the color value in Google Sheets. Can I just point it to another cell to find the value and insert it or do I actually need to find the value?
Thanks for the help and any sort of education would be amazing in this sort because I am trying to learn a little more to stop these questions before they start.
UPDATE:Here is where I have gotten to know and it says when running colorUp that it cannot find the range on line 20 which is
sheet.getRange(rangesMon).setBackground("#f4cccc");
What I don't understand is why it's not finding the range, it's a variable at the global level.
Thanks again for the help.
function colorUp() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Main Board');
var rangesMon = ['D25:D58','D62:D114','D118:D123','D127:D142','D146:D157','D161:D173','D179:D237','D241:D253','D257:D272','D276:D289','D295:D302','D306:D328','D332:D345','D349:D356','D362:D371','D375:D397','D401:D402','D409:D424','D428:D428','D434:D448','D452:D471','D475:D514','D518:D536','D540:D582','D586:D593','D597:D605','D611:D628','D632:D641','D645:D653','D657:D666','D672:D681','D685:D707','D711:D721','D727:D752','D756:D762','D766:D772','D778:D793','D797:D805','D809:D817','D823:D840','D844:D851','D857:D908','D912:D912','D916:D944','D948:I956','D960:D981','D985:D1004','D1010:D1031','D1035:D1051','D1056:D1064','D1069:D1079','D1083:D1094','D1098:D1110'];
var rangesTues = ['E25:E58','E62:E114','E118:E123','E127:E142','E146:E157','E161:E173','E179:E237','E241:E253','E257:E272','E276:E289','E295:E302','E306:E328','E332:E345','E349:E356','E362:E371','E375:E397','E401:E402','E409:E424','E428:E428','E434:E448','E452:E471','E475:E514','E518:E536','E540:E582','E586:E593','E597:E605','E611:E628','E632:E641','E645:E653','E657:E666','E672:E681','E685:E707','E711:E721','E727:E752','E756:E762','E766:E772','E778:E793','E797:E805','E809:E817','E823:E840','E844:E851','E857:E908','E912:E912','E916:E944','E948:E956','E960:E981','E985:E1004','E1010:E1031','E1035:E1051','E1056:E1064','E1069:E1079','E1083:E1094','E1098:E1110']
var rangesWed =['F25:F58','F62:F114','F118:F123','F127:F142','F146:F157','F161:F173','F179:F237','F241:F253','F257:F272','F276:F289','F295:F302','F306:F328','F332:F345','F349:F356','F362:F371','F375:F397','F401:F402','F409:F424','F428:F428','F434:F448','F452:F471','F475:F514','F518:F536','F540:F582','F586:F593','F597:F605','F611:F628','F632:F641','F645:F653','F657:F666','F672:F681','F685:F707','F711:F721','F727:F752','F756:F762','F766:F772','F778:F793','F797:F805','F809:F817','F823:F840','F844:F851','F857:F908','F912:F912','F916:F944','F948:F956','F960:F981','F985:F1004','F1010:F1031','F1035:F1051','F1056:F1064','F1069:F1079','F1083:F1094','F1098:F1110']
var rangesThurs =['G25:G58','G62:G114','G118:G123','G127:G142','G146:G157','G161:G173','G179:G237','G241:G253','G257:G272','G276:G289','G295:G302','G306:G328','G332:G345','G349:G356','G362:G371','G375:G397','G401:G402','G409:G424','G428:G428','G434:G448','G452:G471','G475:G514','G518:G536','G540:G582','G586:G593','G597:G605','G611:G628','G632:G641','G645:G653','G657:G666','G672:G681','G685:G707','G711:G721','G727:G752','G756:G762','G766:G772','G778:G793','G797:G805','G809:G817','G823:G840','G844:G851','G857:G908','G912:G912','G916:G944','G948:G956','G960:G981','G985:G1004','G1010:G1031','G1035:G1051','G1056:G1064','G1069:G1079','G1083:G1094','G1098:G1110']
var rangesFri = ['H25:H58','H62:H114','H118:H123','H127:H142','H146:H157','H161:H173','H179:H237','H241:H253','H257:H272','H276:H289','H295:H302','H306:H328','H332:H345','H349:H356','H362:H371','H375:H397','H401:H402','H409:H424','H428:H428','H434:H448','H452:H471','H475:H514','H518:H536','H540:H582','H586:H593','H597:H605','H611:H628','H632:H641','H645:H653','H657:H666','H672:H681','H685:H707','H711:H721','H727:H752','H76:H762','H766:H772','H778:H793','H797:H805','H809:H817','H823:H840','H844:H851','H857:H908','H912:H912','H916:H944','H948:H956','H960:H981','H985:H1004','H1010:H1031','H1035:H1051','H1056:H1064','H1069:H1079','H1083:H1094','H1098:H1110']
var rangesSat = ['I25:I58','I62:I114','I118:I123','I127:I142','I146:I157','I161:I173','I179:I237','I241:I253','I257:I272','I276:I289','I295:I302','I306:I328','I332:I345','I349:I356','I362:I371','I375:I397','I401:I402','I409:I424','I428:I428','I434:I448','I452:I471','I475:I514','I518:I536','I540:I582','I586:I593','I597:I605','I611:I628','I632:I641','I645:I653','I657:I666','I672:I681','I685:I707','I711:I721','I727:I752','I756:I762','I766:I772','I778:I793','I797:I805','I809:I817','I823:I840','I844:I851','I857:I908','I912:I912','I916:I944','I948:I956','I960:I981','I985:I1004','I1010:I1031','I1035:I1051','I1056:I1064','I1069:I1079','I1083:I1094','I1098:I1110']
for (var i=0; i<rangesMon.length; i++){
sheet.getRange(rangesMon[i]).setBackground("#f4cccc")
for (var i=0; i<rangesTues.length; i++){
sheet.getRange(rangesTues[i]).setBackground("#d9ead3")
for (var i=0; i<rangesWed.length; i++){
sheet.getRange(rangesWed[i]).setBackground("#cfe2f3")
for (var i=0; i<rangesThurs.length; i++){
sheet.getRange(rangesThurs[i]).setBackground("#d5a6bd")
for (var i=0; i<rangesFri.length; i++){
sheet.getRange(rangesFri[i]).setBackground("#fff2cc")
for (var i=0; i<rangesSat.length; i++){
sheet.getRange(rangesSat[i]).setBackground("#d9d2e9")
}
}}}}}}
As someone that never used JS functions in Drive before, I found very simple to understand how it works from this question, so thank you for that :D.
When writing any function, you can use CTRL+space keys to see what commands you have available.
setBackground() works with any written color like "orange", "red", "white" ...
Or a color code like '#000000' to '#ffffff'.
Here is a simple example to color cells A1 to A10.
function test(){
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
sheet.getRange("A1:A10").setBackground('#ffff00');
}
Be sure to keep the same name in your Spreadsheet and in the Script when you call it.
Taking your example, here another way to do it:
var sheet = SpreadsheetApp.getActive().getSheetByName('Main Board');
var rangesMon = ['D25:D58','D62:D114','D118:D123','D127:D142','D146:D157','D161:D173','D179:D237','D241:D253','D257:D272','D276:D289','D295:D302','D306:D328','D332:D345','D349:D356','D362:D371','D375:D397','D401:D402','D409:D424','D428:D428','D434:D448','D452:D471','D475:D514','D518:D536','D540:D582','D586:D593','D597:D605','D611:D628','D632:D641','D645:D653','D657:D666','D672:D681','D685:D707','D711:D721','D727:D752','D756:D762','D766:D772','D778:D793','D797:D805','D809:D817','D823:D840','D844:D851','D857:D908','D912:D912','D916:D944','D948:I956','D960:D981','D985:D1004','D1010:D1031','D1035:D1051','D1056:D1064','D1069:D1079','D1083:D1094','D1098:D1110'];
var rangesTues = ['E25:E58','E62:E114','E118:E123','E127:E142','E146:E157','E161:E173','E179:E237','E241:E253','E257:E272','E276:E289','E295:E302','E306:E328','E332:E345','E349:E356','E362:E371','E375:E397','E401:E402','E409:E424','E428:E428','E434:E448','E452:E471','E475:E514','E518:E536','E540:E582','E586:E593','E597:E605','E611:E628','E632:E641','E645:E653','E657:E666','E672:E681','E685:E707','E711:E721','E727:E752','E756:E762','E766:E772','E778:E793','E797:E805','E809:E817','E823:E840','E844:E851','E857:E908','E912:E912','E916:E944','E948:E956','E960:E981','E985:E1004','E1010:E1031','E1035:E1051','E1056:E1064','E1069:E1079','E1083:E1094','E1098:E1110']
var rangesWed =['F25:F58','F62:F114','F118:F123','F127:F142','F146:F157','F161:F173','F179:F237','F241:F253','F257:F272','F276:F289','F295:F302','F306:F328','F332:F345','F349:F356','F362:F371','F375:F397','F401:F402','F409:F424','F428:F428','F434:F448','F452:F471','F475:F514','F518:F536','F540:F582','F586:F593','F597:F605','F611:F628','F632:F641','F645:F653','F657:F666','F672:F681','F685:F707','F711:F721','F727:F752','F756:F762','F766:F772','F778:F793','F797:F805','F809:F817','F823:F840','F844:F851','F857:F908','F912:F912','F916:F944','F948:F956','F960:F981','F985:F1004','F1010:F1031','F1035:F1051','F1056:F1064','F1069:F1079','F1083:F1094','F1098:F1110']
var rangesThurs =['G25:G58','G62:G114','G118:G123','G127:G142','G146:G157','G161:G173','G179:G237','G241:G253','G257:G272','G276:G289','G295:G302','G306:G328','G332:G345','G349:G356','G362:G371','G375:G397','G401:G402','G409:G424','G428:G428','G434:G448','G452:G471','G475:G514','G518:G536','G540:G582','G586:G593','G597:G605','G611:G628','G632:G641','G645:G653','G657:G666','G672:G681','G685:G707','G711:G721','G727:G752','G756:G762','G766:G772','G778:G793','G797:G805','G809:G817','G823:G840','G844:G851','G857:G908','G912:G912','G916:G944','G948:G956','G960:G981','G985:G1004','G1010:G1031','G1035:G1051','G1056:G1064','G1069:G1079','G1083:G1094','G1098:G1110']
var rangesFri = ['H25:H58','H62:H114','H118:H123','H127:H142','H146:H157','H161:H173','H179:H237','H241:H253','H257:H272','H276:H289','H295:H302','H306:H328','H332:H345','H349:H356','H362:H371','H375:H397','H401:H402','H409:H424','H428:H428','H434:H448','H452:H471','H475:H514','H518:H536','H540:H582','H586:H593','H597:H605','H611:H628','H632:H641','H645:H653','H657:H666','H672:H681','H685:H707','H711:H721','H727:H752','H76:H762','H766:H772','H778:H793','H797:H805','H809:H817','H823:H840','H844:H851','H857:H908','H912:H912','H916:H944','H948:H956','H960:H981','H985:H1004','H1010:H1031','H1035:H1051','H1056:H1064','H1069:H1079','H1083:H1094','H1098:H1110']
var rangesSat = ['I25:I58','I62:I114','I118:I123','I127:I142','I146:I157','I161:I173','I179:I237','I241:I253','I257:I272','I276:I289','I295:I302','I306:I328','I332:I345','I349:I356','I362:I371','I375:I397','I401:I402','I409:I424','I428:I428','I434:I448','I452:I471','I475:I514','I518:I536','I540:I582','I586:I593','I597:I605','I611:I628','I632:I641','I645:I653','I657:I666','I672:I681','I685:I707','I711:I721','I727:I752','I756:I762','I766:I772','I778:I793','I797:I805','I809:I817','I823:I840','I844:I851','I857:I908','I912:I912','I916:I944','I948:I956','I960:I981','I985:I1004','I1010:I1031','I1035:I1051','I1056:I1064','I1069:I1079','I1083:I1094','I1098:I1110']
var ranges= [rangesMon,rangesTues,rangesWed,rangesThurs,rangesFri,rangesSat];
function clear1() {
sheet.clearFormats();
}
function colorup() {
for(var j =0; j<ranges.length; j++){
for (var i=0; i<ranges[j].length; i++){
sheet.getRange(ranges[j][i]).setBackground('#ffff00');
//if empty, print "1";
if(sheet.getRange(ranges[j][i]).getValue() === "" ){
sheet.getRange(ranges[j][i]).setValue(1);
}
}
}
}
Added a new array called range to explain one possible way to use all ranges with the help of arrays.
For every value inside the array, I check if the cell in that range is "empty", if so, the function prints a "1".
I hope this helps you.

Push multi-dimension array into another array

I have an multi-dimension array that i'm iterating through. Is there a way to put the contents of the array into a new multi-dimension array creating a new MDA? For example, the following code puts all indices of the original array into the new candies array if there's a match. Currently i'm doing
candies.push([product[0],product[1], etc...]);
I'm just trying to see if there's a faster/cleaner way to get that in there.
I tried:
candies.push(product);
but that didn't work. Here's the code i'm currently using
var sel = 'candy';
var candies = [];
for(var i = 1; i < products.length; i++) {
var product = products[i];
for(var j = 0; j < product.length; j++) {
if(sel==product[11]){
candies.push([product[0],product[1],product[2],product[3],product[4],product[5],product[6],product[7],product[8],product[9],product[10],product[11],product[12]]);
}
break;
}
}
A cleaner way of writing below line:
candies.push([product[0],product[1],product[2],product[3],product[4],product[5],product[6],product[7],product[8],product[9],product[10],product[11],product[12]]);
is:
candies.push(product.slice(0,13)]);
Sample example JSFiddle is here.
Best of luck.
(Mark this as answer if it serves your need.)

Categories

Resources