Google apps script - Mapping arrays - javascript

I am working on a project where I take multiple column/row inventory sheets and turn them into a multi-row/2-column format for order picking.
I have a switch for selecting the appropriate inventory sheet and a map() function that copies the imported information from the inventory DataRange().
However, not all the data is in consistent columns. What I would like to do is find an expression that maps the next column in if the column it was mapping has a zero or "" value.
I won't give you the full body of code unless you need it, but hopefully just the important parts.
This is what I have:
var source = SpreadsheetApp.openById("1xixIOWw2yGd1aX_2HeguZnt8G_UfiFOfG-W6Fk8OSTs"); //This sheet
var srcSht = SpreadsheetApp.getActive();
var sourceMenu = srcSht.getRange('A1');//This is the cell cotaining the dropdown
var menuTest = sourceMenu.getValue();
// Variable for the vars sheet. If it doesn't exist, create it
var varsTest = source.getSheetByName('vars');
if (!varsTest){source.insertSheet('vars');}
var importedA1 = varsTest.getDataRange().getA1Notation();
varsTest.clearContents();
var t1Imp = '=ImportRange("test1_Id", "Stock!A1:F11")';
var varsData = varsTest.getRange('A1');// This is the cell we fill with the importRange formula
varsData.setValue(t1Imp);
var imported = varsTest.getDataRange().getValues();
var newOrder = imported.map(function(item) {
if (item[4] !== NaN){return [[item[0]],[item[4]]];};
if (item[4] === NaN){return [[item[0]],[item[3]]];};}
var orderRange = source.getSheetByName('Sheet1').getRange(10,1,newOrder.length, newOrder[0].length);
orderRange.setValues(newOrder);
Logger.log("\t" + newOrder);
Logger.log(newOrder):
[(timestamp omitted)] items1,order,caramel,6,c&c,2,mint,3,PB,0,,,items2,,caramel,,strawberry,,mint,,PB,
It seems to be skipping the if statements, or I told it that I mean to test the index as NaN, which will obviously never be true.
I also tried replacing 'NaN' with 'undefined'. Same result. I tried finding the item[4].Values, but it gave me an error. I also tried the same logic using filter() instead of map() but it copied the entire data set.
I pull these values onto a new 'vars' sheet in the workbook (to minimize calls to the web service):
test1
reduce them to the first and last columns, then output:
test
The cells in the 'order' column for the second set of items in the 'test' sheet are blank. The values for that order column should be in item[3] of that array, but I can't get the script to identify that that the blank cells are blank.
I am new to Google Apps Script and JS, but I am watching a lot of tuts and learning by doing. If I find a solution, I will post it.
Thank you StackOverflow, I could not have learned as much as I have without this community!

I have a working function that does what I want. In short:
I had to create a duplicate of the order column in a new column, so that all the values would line up. It's not technically a JS answer, but was the simplest and follows good spreadsheet rules.
function rmZeroOrderPS(item){
var source = SpreadsheetApp.openById("<sheetId>"); //This sheet
var varsTest = source.getSheetByName('vars');
var imported = varsTest.getDataRange().getValues();
var i=-1;
while (i <= imported.length){
if(item[8]!= 0) {return [item[0],item[8]]};
i+=1;
};

Related

Google Apps Script - Defining a Range List with Another Variable

I'm pretty new to this, so I'm not even sure if this is the most efficient way to do this, but I'm trying to save some space in my Google Apps Script by using a cell value to define a Range List.
Here's the code that I have right now:
function myFunction()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var aItems = s.getRange('F1').getValue();
var aItemList = s.getRangeList([aItems.getValue]);
Logger.log(aItemList);
}
The variable aItems is getting the the value of cell F1 on my sheet, which is 'E2', 'E5' (I also tried removing the single quotes surrounding the cell numbers, but that didn't change the result)
With aItemList I am attempting to create a Range List using the value of aItems to define the range. I get an exception error when I run the script stating that the range is not found.
Ultimately the purpose of the aItemList variable will be to have a variable that is storing a list of cells containing checkboxes that I can alternate between being True and False. On the actual sheet that I plan on using this for, there will be hundreds of checkboxes, so I want to avoid having to list them all out in the script as part of the array. I mention this because I have tried variations of this code that have successfully logged aItemList as the correct string, but do not allow me to set the cells value to true or false using aItemList as a reference.
If someone could let me know if this is even possible or not, I would greatly appreciate it. And/or if there is an even better method of accomplishing this task of storing specific cells into a variable as an array that would also be highly appreciated.
Try this:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var aItems = s.getRange('F1').getValue().split(',');
Logger.log(aItems);
let rgl = ss.getRangeList(aItems);
Logger.log(rgl.getRanges().map(r => r.getA1Notation()));
}

Exception: Range not found. Trying to place multiple columns using an active cell. (however works when manually entered)

I'm trying to insert values into cells. Here is my code:
var values = [
["test1", "test2", "test3"]
];
var ss = SpreadsheetApp.getActiveSpreadsheet()
// var sheet = ss.getSheets()[0]
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
var cellsToWriteTo = []
function testFunction() {
var activeCell = sheet.getActiveCell();
var firstRow = activeCell.getRow()
var firstColumn = activeCell.getColumn()
cellsToWriteTo = `["R${firstRow}C${firstColumn}:R${firstRow}C${firstColumn + 2}"]`
console.log(cellsToWriteTo)
var range = sheet.getRange(cellsToWriteTo)
range.setValues(values);
}
This gives me the error:
Exception: Range not found
However if I copy and paste cellsToWriteTo from the console log and put it into getRange.. it works perfectly every time..
Things i have tried so far:
Thought it was to do with not fetching the spreadsheet correctly (getActiveSpreadsheet().getActiveSheet()).
Fixed how my values were organised (into arrays within arrays)
Googled the error message and looked at how to properly getRange/setRange in the documentation and in tutorials. Apparently you cannot set arbitrary cells if you are calling them from the excel sheet itself. This is my suspicion as to what is going wrong here. However how can this be the case if when I put in a simple string it functions fine. I am simply doing string interpolation here.
I know this is rudimentary stuff, any help would be appreciated.
Removing both brackets and quotation marks should make it work
Modification:
cellsToWriteTo = `R${firstRow}C${firstColumn}:R${firstRow}C${firstColumn + 2}`
Execution:
Output:

Inconsistent problems with Google Sheets data mover script

After around two to three hours of digging across many sites, I cobbled together this functioning script to watch an "input" column on a sheet and, using onEdit(), whenever data is put into that column, move the data further down the sheet to the next available cell in that specific row.
function onEdit(e) {
var s = e.source.getActiveSheet();
var sheetName = 'Pricing Chart';
var colToWatch = 5;
var copyFrom = 5;
var nextEmptyCol = colToWatch + 22;
var emptyCellCheck = s.getRange(e.range.rowStart, nextEmptyCol,1,1);
if (s.getName() !== sheetName || e.range.columnStart !== colToWatch) return;
while (emptyCellCheck.isBlank() !== true){
nextEmptyCol++;
var emptyCellCheck = s.getRange(e.range.rowStart, nextEmptyCol,1,1);
}
if (emptyCellCheck.isBlank()){
s.getRange(e.range.rowStart, copyFrom,1,1)
.copyTo(s.getRange(e.range.rowStart, nextEmptyCol,1,1), {contentsOnly: true}),
s.getRange(e.range.rowStart, copyFrom,1,1).clear({contentsOnly: true});
}
}
Specific example:
Into cell E5, type "222". If AA5 is empty, it will copy "222" into AA5, and then clear E5. If AA5 is not empty, it will check AB5, then AC5, etc until it finds an empty cell. On my specific sheet, it then uses all the data from the row and displays various calculations (average, max, etc) so that those data are visible, but all the individual inputs are tucked away behind the scenes. It's been useful for keeping a large list of data on many different variables in a format that's easy to look at and easy to share with others.
Problem is... sometimes it will do as intended at first, find the next available cell, copy the data, and erase the original input cell (e.g. E5). But, sometimes, it will also erase the cell the data was copied TO (e.g. AB5). Roughly once or twice every ten iterations of the script.
So, I was wondering if anyone could have a look at my script and give me tips on optimizing it or just doing things better so the script runs correctly consistently.

Concatenate tables in one and insert it in Google Spreesheet

I've got this basic script for Google Sheets, in G.Script
var a = [['1','2']];
for (var i = 0; i <5; i++){
var range = sheet.getRange("A"+i+":B"+i);
range.setValues(a);
}
As you can see, I write 5 times the table "a" in the sheet. What I'd like in concatenate all the tables "a" in one table and write this table, once in the sheet.
I've tried with concat(), but it does not work. So maybe I don't use it properly.
Could you please help me ?
Many thanks
B.
I recommend that you create your spreadsheet range based on the size of your array "a". Rather than creating a cell range such as A1:B5, I recommend using number values to define the range that you would like to update.
Also, updates to Google Sheets run much faster if you update an entire range at one time.
function myFunction() {
var data = [];
data.push([1,2]);
data.push([2,3]);
data.push([3,4]);
if (data.length > 0) {
SpreadsheetApp.getActiveSheet()
.getRange(1,1,data.length,data[0].length)
.setValues(data);
}
}

Google apps script - Importing sheet values from another sheet

I'm pulling my hair out a little trying to write a conditional Google apps script for Google sheets. The sheet has 5 tabs. Basically, the sheet gets it's data from a google form. Clients fill the form out and it populates our "Master" tab of the sheet in question.
Here's where I'm hung up:
The form is used for teachers to refer students to specific administrators. Whoever is controlling the form needs to be able to assign a specific row of data to a specific administrator, which each have their own tab set up. They do this by selecting, from a drop down, which admin they want to assign the row to.
So, if row 2 is assigned to "Miranda," Certain data would get moved over to the "Miranda" tab. For the sake of argument lets say column D and E of that particular row would get moved over to column A of the "Miranda" tab.
Here's what I've come up with so far:
function importStudent() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var master_sheet = ss.getSheetByName("Master");
var miranda_sheet = ss.getSheetByName("Miranda");
var columnT = master_sheet.getRange("T1:T").getValues();
var columnD = master_sheet.getRange("D1:D").getValues();
var columnE = master_sheet.getRange("E1:E").getValues();
for(var i = 0; i <= columnT; i++) {
if (columnT[i][0] === "Miranda") {
var miranda_row = (miranda_sheet.getLastRow() + 1);
var active_row = SpreadsheetApp.setActiveRange(columnT[i].getRow());
var selectionFirst = columnD.setActiveRange(active_row:columnD);
var selectionLast = columnE.setActiveRange(active_row:columnE);
var fullName = selectionFirst + " " + selectionLast;
fullName.copyValuesToRange(miranda_sheet, 1, 1, miranda_row, miranda_row);
}
}
}
The below code is broken, and i'm looking for a little guidance in making it work. Right off, I know I have at least two issues: 1 there are too many variables, and 2 I'm not sure I can pass a variable as a parameter for the setActiveRange() class. So, my questions:
How can I clean this up? And what (class?) should I be using in order to make it functional?
PS - I still consider myself a novice when it comes to JavaScript. I have the knowledge, but the practical application is something I'm still learning :)
not sure if you need to use script to do what you are trying. Why not use a query formula?
For example:
=query(MasterData!:A:E,"SELECT D,E WHERE A = 'Miranda'",0)
There is some help on queries here. You can also query across spreadsheets. That is to say you could assign Miranda her own spreadsheet, which feeds from your master data.
Please share with me a sample spreadsheet data if you need more assistance.

Categories