Manipulating a Form's data in Google Sheets - javascript

I'm using Google Drive/Sheets to host my personal film ratings list, and I thought it'd be a lot easier than this.
I've set up a form which asks about the film and what rating I give it (out of 4 categories), and then it puts this into a sheet. The data is inputting fine, but I want the sheet to automatically find out the average of my 4 individual ratings, and then add an overall rating to that row of data.
What I can work out...
What I would like...
I'm assuming it requires some kind of trigger from the form submission which then performs some javascript and inputs a new piece of data in the appropriate column and row, but I just want work it out. Any help appreciated.

Navigate in top menu to Tools->Script editor in Summary spreadsheet
Place this code inside
function setAvarage() {
var summaryCloumn = 10 //row J
var sheet = SpreadsheetApp.getActiveSheet();
var lastRow = sheet.getLastRow();
sheet.getRange(lastRow, summaryCloumn).setFormula("SUM(E"+lastRow+":H"+lastRow+")/4");
};
Now setup the trigger in very same script editor Materials->Triggers (first option - unfortunately I do not have docs in english so name can be slightly different..)
Choose add trigger
"setAvarage", "From table", "on change"
just tested and worked...

Related

How to temporarily change color of active cell using Apps Script

So this is an interesting one...in most simple terms - I have a script that jumps me to a specific cell (based on matches in both the column and row - script below for context):
function Jump(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("In-Office"); // change to sheet containing dates
var r = s.getRange("ah1").getValue(); //change A1 to cell containing =match formula
var row = s.getRange("ai1").getValue();
s.setActiveSelection(s.getRange( r + row ));
}
I have this running from a custom menu, where the user can jump to the cell that contains today's date.
I can use conditional formatting to highlight that cell, but there's been an idea I've wanted to execute in other projects and it feels now would be the time to try it out.
Basically, when the cell jumps, I want the COLOR to change for 10 seconds, then reset itself.
I've tried doing this with macros, but the color change is instant, so you never see it.
I tried implementing a "wait period" similar to how you would while showing a TOAST alert, but wasn't able to figure it out.
My other thought is.. setting up a conditional formatting rule through the script, delaying it for 10 seconds, then deleting it. Seems like a lot to just color the activated cell for a specific amount of seconds.
Anyone ever try something like this before? Essentially, I want to flash a different color for the active cell - I think it could be applicable to many other projects as well.
Here's one way to handle that:
const targetCell = 'B12'; // I'm being lazy here, but hoping you get the idea
const range = s.setActiveSelection(targetCell);
const bgColor = range.getBackground();
range.setBackground('red');
SpreadsheetApp.flush();
Utilities.sleep(10000);
range.setBackground(bgColor);
SpreadsheetApp.flush();

Create a custom menu in Google Sheets / Apps Scripts to hide and unhide column based on who is browsing the sheet

I have a massive Google Sheet report that contains a lot of metrics.
A lot of people in my company have access to this report and they all want to see different metrics.
So, I'm trying to create a button at the top of the report that hide/unhide columns depending on the person who is browsing the report.
function onOpen(){
const ui = SpreadsheetApp.getUi();
ui.createMenu('Column Creation')
.addItem('Add Column', 'insertColumn')
.addToUi();
ui.createMenu('View 👀')
.addItem('Patrick View', 'HideColumns')
.addItem('Karen View', 'HideColumns')
.addItem('Umesh View', 'HideColumns')
.addItem('Unhide everything', 'HideColumns')
.addToUi(); function insertColumn()
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('KW'); [...] //this is another function to insert a column
I know I need to use sheet.hideColumns(1) and sheet.showColumns (2), but I'm not sure how to aggregate all of this. Should I create different variables for each view?
Also right now, my code is running both actions at the same time (column creation + hide/unhide columns) how can I separate this?
To summarise I want:
One custom menu "View 👀" with 2 items:
"Patrick View": Unhide all columns but hide column 1 & 2
"Karen View": Unhide all columns but hide column 4 and 6
One custom menu "Column Creation" with 1 item:
"Add column" (this script is already working but running at the same time as the first custom menu so when I click, it both hide column and create a new one)
I hope my explanations are not too messy, thanks in advance for any help !
Try
var sh = SpreadsheetApp.getActiveSheet()
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('Column Creation')
.addItem('Add Column', 'insertColumn')
.addToUi();
ui.createMenu('View 👀')
.addItem('Patrick View', 'hideColumnsP')
.addItem('Karen View', 'hideColumnsK')
.addItem('Unhide everything', 'showColumns')
.addToUi();
}
function showColumns() {
sh.unhideColumn(sh.getRange(1, 1, 1, sh.getLastColumn()))
}
function hideColumnsP(){
showColumns()
sh.hideColumns(1,2)
}
function hideColumnsK(){
showColumns()
sh.hideColumns(4,3)
}
Note that only one person will be active at once!
It is not possible to hide / unhide columns only for certain users
Once a user unhides a hidden column, this column will become unhidden for all the others viewers as well (after a propagation time of few seconds).
A solution would be for each user to have their personal spreadsheet that imports data of interest from the master spreadsheet via ImportRange. That would be optimal for viewing. However, if the users are also meant to modify the data, things will become more complicated since the modified data has to be synched back to the master spreadsheet and conflicts can arise if multiple users modify data simultaneously in their local spreadsheets.

Hiding Rows using Google Apps Script

I'm looking to create almost a drop-down feature in Google Sheets. I was hoping to have arrows set that could potentially collapse rows when clicked.
It would be amazing if I could get it so each main "title" row has an arrow, that would cause the rows to either hide/unhide.
Since only the titles would be written in column A, I was thinking it might be possibly for a script to recognize the blank cells in between two filled ones?
So far I only have this, which is basically useless:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Hides the first three rows
sheet.hideRows(1, 3);
In a perfect world, the spreadsheet would look something like this, with the functionality to collapse the rows with the click of the arrow.
https://docs.google.com/spreadsheets/d/1-oGawFoPXjdHvadxXsSFxuZWWdOxN9ZgbvS10jelWZY/edit?usp=sharing
Not sure if this is possible, but if there is any hope/ideas I'd love to hear them.
function onEdit(e) {
const sh=e.range.getSheet();
const shts=['Sheet13'];
if(shts.indexOf(sh.getName())!=-1 && e.range.columnStart==1 && e.value=="TRUE") {
e.range.setValue("FALSE");
sh.hideRows(Number(e.range.rowStart));
e.source.toast('Row ' + e.range.rowStart + ' has been hidden.');
}
}
Animation:
Instead, have you considered using the group row facility? You can just group and ungroup the rows to show/hide them.
Just select all the rows that you want to group,
right click select Group rows.
I have added the same in the spreadsheet that you have shared.

Google Sheets: Replace cell contents with another cell contents when "Clicked"?

Google Sheets: Replace cell contents with another cell contents when "Clicked" ?
I'm trying to create an easy way to reveal more data from a row when any cell is clicked from the row it lives in. Currently, I have a nice =VLOOKUP("*") ... function that allows me to type in a COMPANY name into a "search bar" cell, and then VLOOKUP will display cell contents depending on which COMPANY it is.
You can see it in action here:
Google Sheets Demo
How To Use:
Green Cells: Type in the "Search Bar" cell for a company name...
Orange Cells: If "COMPANY" is found, it displays the companies cell contents to the left.
What I want to do:
Click on a cell in any row
Have that cell realize what row it's in and navigate to the COMPANY column.
Copy the cells contents from the COMPANY column and replace the current contents of the "search bar" cell
VLOOKUP will then handle the rest.
Possible?
Is this possible with Google Sheet functions alone? Or is this something that should be handled with a script? How would I got about it?
There is currently no way to create an onClick function. The closest thing we have is an onEdit() function. I can think of two ways to make this sort of work.
1) This onEdit version works only if you don't mind locking your script down. You could write an IF statement to create a way to stop this from running temporarily while you edit it.
2) You could setup a second sheet and have the values imported from there or backed up there, and then just run this on the first one at all times.
function onEdit(e) {
//This sets up the worksheet variables
var worksheet = SpreadsheetApp.getActive();
var sheet = worksheet.getActiveSheet();
//This saves the original value e
var origValue = e.oldValue
//This figures out where you clicked, and gets the K+rowyouclicked value.
var selection = sheet.getActiveCell().getA1Notation();
var splitselection = selection.match(/([A-Za-z]+)([0-9]+)/);
var CompName = sheet.getRange("K" + splitselection[2]).getValue();
//set the value on the search bar and return the original value to where you changed.
sheet.getRange("C3").setValue(CompName);
sheet.getActiveCell().setValue(origValue)
}

button adds a row and inputs cell values from text fields

i asked a similar question before however the solution no longer works for my application, i need a button click to create a new row (FailureInstance) in a table (failuretable) and i need it to populate three of the cells with data from fields that are elsewhere filled in. here is my code: form1.failuretable.AddFailureButton::click - (JavaScript, client)
xfa.host.messageBox("Failure Recorded and Added To Table Below. Please Continue Filling Out the Form Until All Failures Have Been Recorded. Then Please Save and Submit the Form.", "Continue/Save/Submit", 3);
if (xfa.host.version < 8) {
xfa.form.recalculate(1);
}
var newRow = failuretable._FailureInstance.addInstance(1);
newRow.FailureCode.rawValue = form1.FailureType.rawValue;
newRow.Location.rawValue = form1.CellLocation.rawValue;
newRow.Comments.rawValue = form1.AdditionalComments.rawValue;
right now this doesn't even create a new row for my table... any help is appreciated!
You should check whether multiple instances for a row are allowed. Select FailureInstance in hierarchy view and then in Object->Binding (if you dont see it go to window menu and select Object) check wheter "Repeat Row for Each Data Item" is selected. If not then select it and your code should work fine.
I advice also to enable JavaScript debugging in your Adobe Reader because it than you should see when error appears and what is it about. Open Reade go to Edit->Preferences->JavaScript and select "Show console on errors and messages". Then you will need to restart Designer.
At design time remember to enable:
Object > Data Binding > Repeat Table For Each Data Item
In code, I believe that lack invoking "instaceManager":
...
var newRow = failuretable.FailureInstance.instanceManager.addInstance(1);
...
http://help.adobe.com/en_US/livecycle/9.0/designerHelp/index.htm?content=000178.html

Categories