Hide Rows and Columns That Are Empty - javascript

On Open, In Worksheet "Time per Job & Job Costing", if Row from column A is empty or includes a 0 value hide it. If Column from row 1 is empty or includes a 0 value hide it.
I looked around and can find techniques for hiding rows if a column is empty but not the other way around, nor if the value is 0.
I have the Rows accomplished with this, but it only works on empty rows:
function onOpen(e) {
["Time per Job & Job Costing"].forEach(function (s) {
var sheet = SpreadsheetApp.getActive()
.getSheetByName(s)
sheet.getRange('A:A')
.getValues()
.forEach(function (r, i) {
if (!r[0]) sheet.hideRows(i + 1)
});
});
}

If Column from row 1 is empty or includes a 0 value hide it.
Explanation:
I believe your goal is to hide a column if a cell of that column
in the first row is empty or 0. You also want to achieve that when you open the spreadsheet file.
To achieve that, the first step is to get the values of the first row. You can use getRange() to get the first row starting from the first column until the last column with content. For the latter you need to use getLastColumn(). Then you can flatten the array in order to perform a forEach loop. For every element in the first row, you check whether it is empty or 0. If this condition evaluates to true then you hide that particular column.
Solution:
function onOpen() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Time per Job & Job Costing');
const first_row = sh.getRange(1,1,1,sh.getMaxColumns()).getValues().flat();
first_row.forEach((fr,i)=>{
if(fr=='' || fr==0){
sh.hideColumns(i+1);
}});
}

Related

I need help creating a script to hide columns in google sheets if cells in range are empty

I am trying to create a script which will hide columns where cells in a range are empty (the condition).
Below is what my table looks like, you can see there are 12 roles. I basically want to hide any columns from Role 4 onwards which are unused e.g. no values in the 3 rows.
i have a script that works (see below) but it also hides Roles 1, 2 and 3 if they are blank. I want the script to only work from Role 4 (column F) onwards. What do i need to change in the script for this to work?
P.s. I have used code from other posts to put this together so there may be lines in the code I do not need, I am still very new to google script.
function hidecolumns() {
var sh = SpreadsheetApp.getActiveSpreadsheet();
var ss = sh.getSheetByName("Project Team Resources");
var r = ss.getRange("C5:N10");
var data = r.getValues();
var rData, cData, x;
cData = data[0].map(function (col, c) {
return data.map(function (row, r) {
return data[r][c];
});
});
Logger.log(cData.length);
Logger.log(data.length);
for(var i=0;i<cData.length;i++){
if(cData[i].filter(String).length==0)
ss.hideColumns(i+3)
}
}
Explanation:
Your goal is to hide a column if it is empty with the exception of the first three columns in the range.
One approach to achieve this to iterate over all columns in the desired range and check if they contain at least one non-empty value (length>0). If the length is 0, meaning that all values in this column are empty, then hide it.
In more detail, I use a forEach loop to iterate over each column. Each column is given by a simple map expression:
data.map(d => d[col]) where col takes values 0, 1, 2, ...
I filter on the non-empty values and hide only columns if the length of the non-empty array is 0.
Since the data range starts from column C which is the 3rd column, I hide column col+3. Be careful with this, since it depends on the input range.
Finally, your goal is to keep the first 3 columns (Roles 1,2,3) fixed-unhidden regardless if they contain values or not. To do that, add an extra condition in the if condition to check if column is larger than 2: col>2.
Solution:
function hidecolumns() {
var sh = SpreadsheetApp.getActiveSpreadsheet();
var ss = sh.getSheetByName("Project Team Resources");
var data = ss.getRange("C5:N10").getValues();
data[0].forEach((_,col)=>{
if(data.map(d => d[col]).filter(e=>e!='').length==0 && col>2){
ss.hideColumns(col+3);
}
});
}
underscore as a parameter reference

How do I get the values of my spreadsheet columns

I'm new to apps script and javascript but I managed to get all the defined value numbers or column numbers right except for one that is about 200 columns to the right. But column numbers don't always ad up as it seems like it skips some empty columns sometimes but not others for some reason. Is there a way to number the columns with a script? I found this one which runs successfully but I don't know how to print the result.
function getValue() {
var values = SpreadsheetApp.getActiveSheet().getDataRange().getValues()
}
function numberColumns() {
const ss=SpreadsheetApp.getActive();
const sh=ss.getActiveSheet();
const rg=sh.getRange(1,1,1,sh.getMaxColumns());
var v=rg.getValues()[0].map(function(e,i){return i+1;});
sh.insertRowBefore(1);//inserts a row first to hold column numbers
rg.setValues([v]);
}

tabulator update column data

I've a table set up with Tabulator with unknown data ( pulled from a file) so i can't make a column definitions setup for it except for general setup that works with any kind of data.
now I'm in situation that i need to add certain code for each row under column code which is included with the existing table.
i looked for away to add code something like SX0001 on the first row and just add 1 each row so the second row would be look like SX0002
I've checked this link http://tabulator.info/docs/4.1/update#alter-replace i saw some functions that works on rows but not columns .
solution i'm trying to achieve:-
the link includes under updateData function that i need to have index with default value (Id)
giving that I'm not sure what type of date it might have or be included with the table i've added a new column using this code
table.addColumn({title:"id", field: "id" , formatter:"rownum",width:40, align:"center"} , true)
now I don't know how to get the Length of the Column to loop through each one of the column and run function like this
var no = 1000;
var str = "SX";
var x = str + no ;
table.updateData([{id:1, code:x}]);
var no = no +1;
is there's anyway to do this ?
You can retrieve the data stored in the table using the getData function.
var data = table.getData();
This will return an array containing the data objects for each row in the table.
Then to get your lenght just do data.length
If you want to count the table data the getDataCount exactly for this, which returns a count of rows in the table:
var count = table.getDataCount();
But actually your current approach will result in the table being redrawn a load of times as you update the row data.
Mutators
Instead you should look at using a Mutator, these allow you to alter data as it enters the table, so in this case you could do something like this:
//global variable to keep track of row id
var rowCount = 0;
//custom mutator
var idMutator = function(value, data, type, params, component){
//increment rowCount and assign as column value
return rowCount++;
}
//in your column definition set this mutator
{title:"ID", field:"id", mutatorData:idMutator}
By using this approach the value is set as the data is loaded into the table, nothing needs to be redrawn and the whole proceess is much more efficient

Cut and paste data range to bottom of another sheet

I'm new to google app script and not sure how to solve this issue.
I have two sheets, "Main" and "Extract". Throughout the day, data is pasted into the "Extract" sheet, cleaned up (script removes certain columns and removes rows where a value in column A also exist in column A of the "Main" sheet), and then added to the "Main" sheet, which contains all of the cleaned up extracts pulled throughout the day.
I am trying to write a script that will take all of the cleaned up data in the "Extract" sheet and cut and paste it beneath the last row with data in the "Main" sheet.
Once cleaned up the "Extract" sheet always has 8 columns (A-H), but the amount of rows varys each time. I do not need the header or column 8 (H) pasted to the "Main" sheet, only A2:G (stopping at the last row with data).
So far this is all I have been able to come up with (it is likely very wrong)
var data = sheet.getDataRange().getValues()
.offset(0, 0, 1);
I believe this is what you are trying to do.
var spread = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spread.getSheetByName("Extract");
// skip row 1 (header) and beyond column G
var range = sheet.getRange(2,1,sheet.getLastRow()-1,7);
sheet = spread.getSheetByName("Main");
var rows = sheet.getRange(1,1,sheet.getLastRow(),1).getValues();
// search for first blank row column 1
for( var i=0; i<rows.length; i++ ) {
if( rows[i][0] === "" ) {
var offset = sheet.getRange(1,1).offset(i,0);
range.copyTo(offset);
break;
}
}

SharePoint - Use JavaScript to get the list items after filter is applied

Consider I have a list called "test"
And it has 5 list items (say a, b, c, d, e)
Now that I apply filter manually on the list items for a particular field
Now the result is 3 list Items.
Now I want to read the fields of these 3 list items through JavaScript & do calculation on it.
I need to access the fields of the currently displayed (after filter is applied manually)
I do not want to filter it in Javascript. But I want to filter some field manually & then I want to read the displayed result. Can anyone help ?
The following jQuery will get all the values for a column for the rows that are displayed. It works on SharePoint 2010. I left the code very verbose and with debugging statements so it's easier to understand. Keep in mind that this code will not run in IE with the console closed unless you remove the console.log and debugger lines.
$(document).ready(function() {
var columnHeading = "Title";
var anchor = $("a").filter(function() {
return $(this).text() === columnHeading; //find the link that matches the text in the columnHeading variable
});
var th = anchor.closest("th"); //Get the parent table header
var columnIndex = th.index(); //Get the index of the table header in the row for use on other rows
var table = th.closest("table");
var i;
debugger;
table.find("tbody").first().find("tr").not(":first").each(function() { //Loop over every row in the table EXCEPT the first row because its the table header
i = $(this).find("td").eq(columnIndex); //find the td element at the index we determined earlier
console.log($(i).text()); //get the text in the td. You may need to parseInt before any mathematical formulas.
});
});
If you need clarification on anything, please let me know.

Categories

Resources