Can we hide column in sheet using google apps script? - google-apps-script

I want to share the sheet with others but i also want them to not able to see some columns. Is there any way out to solve this using google apps script or if any alternate option for the same. Thank you.

You should take a look at the documentation, it will help you to find the function you're looking for. In this case, you need the Sheet.hideColumns(columnIndex) method
hideColumns(columnIndex)
Hides the column at the given index.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Hides the first column
sheet.hideColumns(1);

Very simple function
function onOpen(){
ss = SpreadsheetApp.openById( 'sheetid' ),
sheet = ss.getSheetByName('sheet_name'),
ss.getActiveCell();
sheet.hideColumns(start_column_number, end_column_number);
}

If the columns to be hidden are part of a merged column set, the hide columns methods don't work unless you hide all the columns in the merge.

Related

Show and hide selected columns with single macro in google sheets

Starting out like all others. I am new to macros and I know what I'm trying to do is easy, but I can't get it working.
So I'm making buttons on a spreadsheet to show and hide columns, if the column is hidden then show, if the column is shown then hide. So a toggle button between show and hide basically.
I will continue to research while this is here as I'm sure the answer is simple. Just an If, then, else but I'm a noob.
Any help would be appreciated. Here's one of the 'show' ones I have if it's easier to edit that.
Thanks
function ShowCA() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getActiveSheet().showColumns(6);
};
I believe your goal is as follows.
About the column "F", when the column is showing, you want to hide the column. When the column is hiding, you want to show the column.
You want to achieve this using a function of Google Apps Script.
In this case, I thought that isColumnHiddenByUser can be used. So, how about the following sample script?
Sample script:
function ShowCA() {
var column = 6; // This is column "F".
var sheet = SpreadsheetApp.getActive().getActiveSheet();
if (sheet.isColumnHiddenByUser(column)) {
sheet.showColumns(column);
} else {
sheet.hideColumns(column);
}
}
When this script is run, when the column "F" is showing, the column is hidden. When the column "F" is hiding, the column is shown.
References:
isColumnHiddenByUser(columnPosition)
showColumns(columnIndex)
hideColumns(columnIndex)

Function to find cell with certain value in whole worksheet

So I've got a simple problem that I cannot seem to find a simple solution to. Basically what I want to do is have a formula that will find the first instance of a specific value (e.g "ADC") that occurs in a certain range (A1:Z1000), and give me the A1 notation for that cell's position in that sheet. I've tried Index&Match but that is column/row dependent, Regex, Find, etc. but none of those work. I just want it to go cell by cell and report back the absolute location (in the sheet) of the cell that has the first instance of that value. Any help would be appreciated. Thank you
function NEEDLEINHAYSTACK(needle) {
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
var rg=sh.getDataRange();
var r=rg.createTextFinder(needle).findNext();
return r.getA1Notation();
}

Vertically Align Text on google spreadsheet via google script

I am trying to figure out how to align text in the middle vertically. According to the documentation I have found my following example should work:
var form = FormApp.getActiveForm();
var ss = SpreadsheetApp.openById(form.getDestinationId());
var sheet = ss.getSheets()[0];
var fullRange = sheet.getRange("A1:Z1001");
fullRange.setHorizontalAlignment(DocumentApp.HorizontalAlignment.CENTER);
fullRange.setVerticalAlignment(DocumentApp.VerticalAlignment.CENTER);
The interesting thing I find is that the Horizontal Alignment works, but the vertical one does not. Am I possibly using out dated methods? Is there a better way to accomplish this?
Just use middle as the parameter for setVerticalAlignment() :
fullRange.setHorizontalAlignment("center").setVerticalAlignment("middle");
You're using the Enum for the DocumentApp intended for Google Docs tables and cells and you're using Spreadsheets.

How to remove data validations?

I want to remove all data validations from a sheet, tried following but it does not get rid of them. Anyway to do this through apps script?
var ss = SpreadsheetApp.getActiveSpreadsheet();
var accountSheet = ss.getSheetByName("Account A");
accountSheet.getRange(1,1,274,61).clear();
accountSheet.getRange(1,1,274,61).setDataValidation(null)
That should do the job.
A more generic answer to remove all data validations from the whole sheet (remove from all cells in sheet):
mySheet.getRange(1,1, mySheet.getMaxRows(), mySheet.getMaxColumns()).setDataValidation(null);
You could in theory use this less verbose version (might be faster because it affects a smaller range of cells):
mySheet.getDataRange().setDataValidation(null);
However, there may be empty cells outside the DataRange that do have validations and those validations will not be removed (they are outside the scope of DataRange, won't be selected by DataRange). Hence, you should use the first version I mentioned for more robust code: it selects all the cells in the sheet and removes validations.

Google Spreadsheet - Insert cut/copied cells/rows

Google Spreadsheet doesn't have the functionality to "insert cut cells" like in Excel.
Let's say I selected A4 to B5, hit Ctrl+X.
Then I moved the selection to A2.
Now I want to "insert cut cells", probably by inserting blank cells and moving the dotted-range to A2.
Is there any way to do it using JavaScript on your own menu?
eg.
function insertCutOrCopiedCells(){
var SS = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SS.getActiveSheet();
var cell = sheet.getActiveCell();
// How do you get the cells being cut/copied? (ie. A4 to B5)
// We can then insert empty cells (2 cols 2 rows) at the selection (A2)
// and move the cut cells (A4 to B5) there
}
To achieve the insert cut cells feature in google sheets you simply use drag and drop. First highlight the row(s) you want to move then mouse over the row number (you will see a hand icon); then, holding your left-click, and keeping your mouse over the row numbers, you will see a dark line where the insert will take place. Let go of your mouse button and voila.
You have both methods, check'em in the class Range.
moveTo(target)
Cut and paste (both format and values) from this range to the target range.
copyTo(destination)
Copies the data from a range of cells to another range of cells. Both the values and formatting are copied.
Edit:
To complete the function you'll have to use also:
insertRowsAfter(afterPosition, howMany) in class Spreadsheet
Inserts a number of rows after the given row position.
and
getActiveRange() with getRow() to check where's the selection at:
Returns the range of cells that is currently considered active. This generally means the range that a user has selected in the active sheet, but in a custom function it refers to the cell being actively recalculated.
Since you don't have direct acess to the clipboard, you'll have to set up a sidebar, or a modelessDialog, which asks for a range to copy from, and it would paste into the selected area, or the other way around, paste the current selected area onto an inputed ROW.
I think , you can separate the function.
Copy : use getRange with getValue:
link => https://developers.google.com/apps-script/reference/spreadsheet/sheet
Delete data : use the getRange with setValue = blank
example :
var Spreadsheet=
SpreadsheetApp.openById(SPREADSHEET_ID).getSheetByName("number1");
var value = Spreadsheet.getRange(1,0).getValue(); // copy
Spreadsheet.getRange(1,1).setValues(value); // insert
Spreadsheet.getRange(1,0).setValues("");
You can use the metho copyTo
example :
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange("A:E").copyTo(sheet.getRange("F1"), {contentsOnly:true});
}