Is it possible to create Buttons with Google Apps Script? - javascript

I would like to have a list with Names and next to them a Button, that brings me to that Spreadsheet. But i dont want to create a Button for each one manually.
Otherwise i would create a Sidebar with a Dropdown.. but would prefer the first solution :)
Thank you
Now I just have a single Name with a Single Button

Yes, you can insert images with Apps Script and use it as buttons. Use sheet.insertImage() to insert the image and assignScript(functionName) to assign the function.
See example below:
//Function to insert image
function insertImg() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var image = sheet.insertImage("https://www.google.com/images/srpr/logo3w.png", 1, 1); //change the URL to the image you prefer
image.assignScript("sheet"); //assign the function to the image
}
//function that takes you to the sheet you specify
function sheet() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Sheet2'), true); //change Sheet2 to the name of the sheet where you want to go
}

Description
You could simply have a checkbox in the column next to the sheet names. When the user clicks a checkbox the selected sheet will become active.
Then using a simple onEdit(e) trigger keep an eye on the checkbox column.
Simple, low overhead, no image file needed.
Code.gs
function onEdit(e) {
try {
if( ( e.value === undefined ) || ( e.value === "" ) ) return;
if( e.range.getSheet().getName() === "SheetList" ) {
if( e.range.getColumn() === 2 ) {
e.range.setValue(false);
e.source.setActiveSheet(e.source.getSheetByName(e.range.offset(0,-1).getValue()));
}
}
}
catch(err) {
SpreadsheetApp.getUi().alert(err);
}
}
Reference
onEdit() Simple Trigger
Simple Trigger Event Object

Related

Copy data from one spreadsheet to another on edit

I already try everthing but I cant make this work =/
I'm trying to copy data from one spreadsheet to other using setValues(), because link doesn't work for me. I also need to keep the trigger on edit.
So, I create one fuction called AddConvocacao, and always when have any change the script run.
function addConvocacao(e){
var linha = e.range.getRow(); //get the number of the line, where have edit
var nome = e.source.getActiveSheet().getRange(linha,2).getValue(); //get the value of the field
var targetSheet = SpreadsheetApp.openById('1iQbZ7iB9ddc-HwLK9vG0eVEeWWqXKJZ0ry7U_Hm4SkQ') //open the other spreedsheet
var targetName = targetSheet.getSheetByName('Pac'); //open the tab
var lastRow = targetName.getLastRow(); //count the last row in this tab
lastRow = lastRow+1; //add 1
targetName.getRange(lastRow, 2).setValue(nome); // set the value
// targetName.getRange(lastRow, 3).setValue(valorCol7);
// targetName.getRange(lastRow, 5).setValue(dose);
// targetName.getRange(lastRow, 6).setValue(diagnostico);
// targetName.getRange(lastRow, 7).setValue(medico);
}
why dosen't work when I use on edit?
Thanks so much! =)
I did it a little different I think. I put checkboxes in column one and capture the value from column 2 and sent it to the other spreadsheet to the bottom of column 2
function onMyEdit(e) {
//e.source.toast('entry');
const sh = e.range.getSheet();
//Logger.log(JSON.stringify(e));
if (sh.getName() == 'Sheet1' && e.range.columnStart==1 && e.value=="TRUE") {
//e.source.toast('cond');
const v = sh.getRange(e.range.rowStart, 2).getValue();
var tss = SpreadsheetApp.openById(getGlobal('targetid'));
var tsh = tss.getSheetByName('Sheet1');
tsh.getRange(tsh.getLastRow() + 1, 2).setValue(v);
e.range.setValue("FALSE");
}
}
You must use an installable trigger to us openById();
getGlobal(targetid) just gets a spreadsheet id you can replace that with the id
Also I limited the action to only one sheet and only when I set the checkbox to true. I reset it back to false in the code.
You'll need to modify a few things to get it to work for you because when I'm doing the examples I do it my way.

Require fix to script app to move an array from one google sheet to another on a trigger. either through a button or cell input

I'm attempting to create a script that will allow the following steps to occur
1. I input property survey data on Sheet "Inputs" in Array(B4:H4)
2. On Cell (F13) being between 98%-102%, the option (button or cell highlighted(I7)) to ADD to target sheet appears or becomes active
3. on pressing the cell or button data is moved to sheet "New Base(C5:I5)"
4. Data in target Array is deleted
5. On the repeat of the above cycle for new paddocks, data adds below already moved data on Sheet "New Base"
So far I have used a mash-up of different scripts I have found online. I've included the references in them.
My apologies if I've used incorrect terminology. The link to the sheet is below.
https://docs.google.com/spreadsheets/d/1KAXtTKijPjhH70pNe3RHUl5R6UwaJNRVBnqgFx4mmcw/edit?usp=sharing
Any help would be much appreciated for one confused Newby.
function onEdit() {
archiveRows();
clearCells();
}
function archiveRows()
{
var ss=SpreadsheetApp.getActive();
var sh0=ss.getSheetByName('Inputs');
var rg0=sh0.getDataRange();
var sh1=ss.getSheetByName('New Base');
var vals=rg0.getValues();
for(var i=vals.length-1;i>11;i--)
{
if(vals[i][9]=='ADD')
{
sh1.appendRow(vals[i]);
sh0.deleteRow(i+1)
}
}
}
//This could be accomplished with SpreadsheetApp.getActive().getActiveSheet().clear();
function clearCells('B4:H4')
{
//https://stackoverflow.com/questions/9268570/i-need-a-button-to-clear-cells-in-a-google-spreadsheet
var ss=SpreadsheetApp.getActive().getSheetByName('Inputs');;
ss.getRange('B4:H4').clearContent('B4:H4')
}
function projectTriggerExists(functionName)
{
if(functionName)
{
var allTriggers=ScriptApp.getProjectTriggers();
var funcExists=false;
for(var i=0;i<allTriggers.length;i++)
{
var trigger=allTriggers[i];
if(allTriggers[i].getHandlerFunction()==functionName)
{
funcExists=true;
break;
}
}
}
return funcExists;
}

Google script - Hide/Unhide google sheet based on checkbox on another sheet

I"m pretty sure I'm doing something wrong in the code, as to why it's not functioning the way I want it. Firstly, here's the code:
function onEdit(a) {
var sheet = a.source.getActiveSheet();
var aa = SpreadsheetApp.getActiveSpreadsheet();
var COMP = aa.getSheetByName("COMP");
var COMPcell = sheet.getRange('B6').getValue();
if(COMPcell = 'TRUE'){COMP.showSheet();}else{COMP.hideSheet();}
}
In here, I have a checkbox on cell B6 of the 'active sheet' (named Monthly summary). When checked (and thus have a value of TRUE), I want the sheet named "COMP" to appear. Otherwise, it should be hidden. I'm not really good at coding and I've researched the above formula and modified it as to my requirement, but I can't get it to work.
Any insights on this will be very much appreciated. Thanks!
You want to show the sheet of COMP only when the checkbox of "B6" is checked.
You want to hide the sheet of COMP when the checkbox of "B6" is not checked.
If my understanding is correct, how about this modification? Please think of this as just one of several answers.
Modified script 1:
If your script is modified, in your script, COMPcell = 'TRUE' of the if statement is not compared the value. In this case, please modify to COMPcell === true. The modified script reflected this is as follows.
function onEdit(a) {
var sheet = a.source.getActiveSheet();
var aa = SpreadsheetApp.getActiveSpreadsheet();
var COMP = aa.getSheetByName("COMP");
var COMPcell = sheet.getRange('B6').getValue();
if (COMPcell === true) { // Modified
COMP.showSheet();
} else {
COMP.hideSheet();
}
}
Modified script 2:
When the event object and isChecked() are used, your script can also be modified as follows. In this script, only when the checkbox of "B6" is edited, the script works.
function onEdit(a) {
var range = a.range;
if (range.getA1Notation() == "B6") {
var COMP = a.source.getSheetByName("COMP");
if (range.isChecked()) {
COMP.showSheet();
} else {
COMP.hideSheet();
}
}
}
References:
if...else
isChecked()
Event Objects

Google Script array "for" performance

Im working with google spreadsheet, and google script.
I have problem with for() function in array.
Its run 346++ seconds and failed.
I need to match data in two columns, and matched data is written in the same row that found, but another column i working have almost 30,000 rows and it takes forever.
Here is the code im working, i have tested it in smaller scale and its worked:
function check2dArray() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("2d array");
var dataInputValues = sheet.getRange("A:B").getValues();
var dataCheckValues = sheet.getRange("D:D").getValues();
var writeResultColumn1 = sheet.getRange("F:F").getColumn();
var writeResultColumn2 = sheet.getRange("G:G").getColumn();
//clearing data
sheet.getRange("F:G").clear();
for(i=0;i<dataInputValues.length;i++){
for(j=0;j<dataCheckValues.length;j++){
if(dataInputValues[i][0] == dataCheckValues[j][0] & dataInputValues[i][0] != "" & dataInputValues[i][0] != "#N/A" ) {
sheet.getRange(j+1, writeResultColumn1).setValue(dataInputValues[i][0]);
sheet.getRange(j+1, writeResultColumn2).setValue(dataInputValues[i][1]);
}
}
}
}
Im still new in this, maybe there is better way to solve?
Here is the screenshoot of my sheet
Sheet screenshoot
Code screenshoot
Here is Link to my sheet :
https://docs.google.com/spreadsheets/d/1DVbNaehsTWkiIkzW2nQx7w-ZB8CrPmSP5T5CpU24mbU/edit?usp=sharing

Google Sheets activate a cell within a loop

I have a loop that does what I want, but I would like to set an active cell in each sheet so I don't have to click it each time as I cycle through.
I've tried two ways that seem to make sense, but they both only work on the last sheet in the loop:-
function setDraft() {
//msg box to confirm relevant sheets are hidden, thus excluded from code
var response = Browser.msgBox("SET AS DRAFT","Have you hidden sheets you don't want marked as draft?", Browser.Buttons.YES_NO);
if(response=="no")
return;
else if(response=="cancel")
return;
else
//loop and code for Visible sheets
var ss = SpreadsheetApp.getActive();
var allsheets = ss.getSheets();
for (var s in allsheets){
var sheet=allsheets[s]
var date = sheet.getRange('I1')
if (sheet.isSheetHidden()!= true) {
//sheet.setActiveRange(date);
sheet.setActiveSelection(date);
sheet.getRange('I5').setValue('DRAFT');
}
}
}
Can anyone please let me know where I'm going wrong?
Thanks in advance!
Use the sheet.activate() or range.activate() methods. Use sheet method if you only care to go to a specific sheet, but if you want the select a specific cell then use it with range. I have not tested with range, however, I know that if you use sheet.activate() then you will be brought to that sheet.
Using for...in in JS can lead to problems (more on that in this post). Second, try using the built in UI class through the SpreadsheetApp class rather than the Browser buttons.
function setDraft() {
var ui = SpreadsheetApp.getUi();
var allsheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
//msg box to confirm relevant sheets are hidden, thus excluded from code
var response = ui.alert("SET AS DRAFT","Have you hidden sheets you don't want marked as draft?", ui.ButtonSet.YES_NO);
if(response == ui.Button.NO) {
return;
} else {
for (var s=0; s<allsheets.length; s++){
var sheet=allsheets[s]
var date = sheet.getRange('I1')
if (sheet.isSheetHidden()!= true) {
sheet.setActiveSelection(date);
sheet.getRange('I5').setValue('DRAFT');
}
}
}
}

Categories