Google sheet buttons don't work on mobile - google-apps-script

I noticed recently that buttons created in the google worksheet don't work on mobile. I found the resolution, however now I wonder how could I point the code presented in the article to the specific tab in my worksheet, for instance, "sheet 1" and "sheet 2". As I'd like the code to work on both tabs "sheet 1" and "sheet 2", in parallel. So that two different users, can work on two different tabs at the same time.
Would anyone know how to overcome this issue?
https://medium.com/macadamscripts/create-button-in-google-sheets-mobile-2979579025ef#:~:text=Luckily%2C%20there%20is%20a%20workaround,serve%20as%20the%20%E2%80%9Cbutton%E2%80%9D
function onEdit(e) {
if (e.range.getA1Notation() == 'D4') {
if (/^\w+$/.test(e.value)) {
eval(e.value)();
e.range.clear();
}
}
}
Best,
D

Try to use activeSheet.getName in handling/checking a specific sheet on your spreadsheet. See sample code below on how to achieve it:
function onEdit(e) {
//Set a comment on the edited cell to indicate when it is changing.
var range = e.range;
var activeSheet = e.source.getActiveSheet();
if (activeSheet.getName() == "Sheet1") { //Sheet1 is just a sample name of the sheet. Modify it according to the name of your sheet.
range.setNote("Last Modified:" + new Date());
}
}
Reference:
use onedit() trigger on a specific sheet within google scripts for google sheets

Just for anyone else who may read this, Google defines:
Spreadsheet = the actual Sheet Document containing the sub sheets.
Sheet = the "tabs" as Ducatia uses.
Anyway, to answer the question, you could try using this which will set the tab to whatever is specified. You can select a sheet based on index of the array but sheet orders can be moved and unfortunately names can be changed too, but not accidentally like simple sheet reorder, so just need to be careful. You can get the sheet's ID but I can't see a way to select a sheet by ID. If I do I'll update this.
I used something like this:
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName("TEST"));
If you run the above, with a proper name, you can watch your spreadsheet change the UI view to the specified sheet.

Related

Lock a sheet then export URL

Absolute noobie here, just trying to automate a process we currently do manually between google sheets.
I'd like to have a script attached to a button that does the following when pressed -
"lock" a vlookup or freeze a cell's contents. (I use a separate sheet of prices for materials that changes over time. When a quote gets accepted, I want to be able to "lock" the data that's already been pulled across from the other sheet, so the customer only pays what they've been quoted.
export the current sheet URL (or ID) to another sheet. I've got a "dashboard" sheet that we use as an overview of all the other sheets. Once the button is pressed I want to add the current sheet to the list of sheets we are monitoring on the dashboard sheet. Essentially I want to be able to extract the url of the sheet where we've just pressed the button, and add that url (or ID) to a list on another sheet.
Hope that makes sense! Any help is greatly appreciated.
This will get you started. The copyUrl function will paste the url to the first empty row in column B, you can change the column by changing the 2 on codeline 12.
function freeze(){
const ss = SpreadsheetApp.getActiveSpreadsheet();
const activeSheet = ss.getActiveSheet();
activeSheet.getDataRange().copyTo(activeSheet.getDataRange(),{contentsOnly:true});
}
function copyUrl(){
const targetSheetId = 'xxxxxx';
const targetSheetName = 'sheetname';
const currentUrl = SpreadsheetApp.getActiveSpreadsheet().getUrl();
const target = SpreadsheetApp.openById(targetSheetId).getSheetByName(targetSheetName);
target.getRange(target.getLastRow()+1,2).setValue(currentUrl);
}
References
copyTo(spreadsheet)
getLastRow()

Clear cells across two or more sheets

I have the following simple script which deletes a range of cells from one sheet. How can I amend this so that I can specify cells across more than one sheet? I assume it's something to do with changing getSheetByName? Many thanks.
function clearandover() {
//replace 'Sheet1' with your actual sheet name
var sheet = SpreadsheetApp.getActive().getSheetByName('Andover');
sheet.getRange('B8:G').clearContent();
}
If you are just going to do one command on each sheet, it doesn't help you to define the sheet in a separate value. You can just do it like this:
function clearandover() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getSheetByName('Andover').getRange('B8:G').clearContent();
spreadsheet.getSheetByName('Second sheet name').getRange('B8:G').clearContent();
spreadsheet.getSheetByName('Third sheet name').getRange('B8:G').clearContent();
}
Of course you can change the range per sheet that needs to be cleared.

Rename a Google sheet tab based on cell

I partially know the name of 2 sheets I need to rename when the cell B2 change in each sheet:
One in called csv_nat and it should be renamed to csv_nat_30_11_2019 when cell B2 in sheet changes (it is a date format like 30/11/2019)
The other one has the exact same structure and is called csv_alt
My main issue is the following. When the tab name has already changed, I need to locate the sheet from its root name (csv_nat) and I have not figured out how.
My limited and starting code for now is:
function onEdit(e){
if(e.range.getA1Notation() != "B2") return;
e.source.getActiveSheet().setName("csv_nat_"+e.value);
}
Besides, does the onEdit function work for any change or only when user edits the cell?
If you want to locate your csv_nat or csv_alt sheets, you can use regex in this case.
Try this example
if sheet contains csv_nat or csv_alt it will alert you:
var ss = SpreadsheetApp.getActiveSheet();
if(ss.getSheetName().match(/csv_nat|csv_alt/)){
SpreadsheetApp.getUi().alert("Working Sheet is found")
}
Reference
String.match()

Trouble triggering a macro when a specific cell has a specific value

I'm trying to trigger a google sheet macro / apps script to reformat a sheet when a specific value is entered into a specific cell. I have been working on it to get it to work but without success.
Here is my code;
function onEdit(e) {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = SpreadsheetApp.getActiveSheet();
var cell = sheet.getRange('C2');
var cellContent = cell.getValue();
if(cellContent === 'Past campaigns - actual cashflows to date only') {
spreadsheet.getRange('7:12').activate();
spreadsheet.getActiveSheet().hideRows(spreadsheet.getActiveRange().getRow(), spreadsheet.getActiveRange().getNumRows());
spreadsheet.getRange('13:13').activate();
}
};
I'm new to macros and apps scripts. I have tried to implement all the suggestions from the following links without success;
How can I get a macro to automatically trigger when a cell reaches a certain value in a Google Sheet?
https://developers.google.com/apps-script/guides/triggers
https://developers.google.com/apps-script/guides/sheets
As #Cooper has said you cannot make a trigger based on a cell value. Only on user changes.
You could try to make the onEdit trigger to execute only inside an if statement. But that's already what you have done.
Try this:
function onEdit(e) {
e.source.toast('Entry');
const sh=e.range.getSheet();
if(sh.getName()!='Sheet Name')return;//you need to change this sheetname
const X=sh.getRange('C2').getValue();
if(X=='Past campaigns - actual cashflows to date only') {
e.source.toast('flag1');
sh.hideRows(e.range.rowStart,e.range.rowEnd-e.range.rowStart+1);
}
}
It's not necessary to use activate in most scripts and certainly in this one since it has to finish in 30 seconds. They use activate in macros because they are following you actions as you move around the screen. But in a script, generally to don't want to interact with the screen very much because it slows down the script and it doesn't really accomplish anything.
I'm currently using in a script that run when a Spreadsheet opens up because it has a lot rows in it and I want it to go down to the last row. So activate is useful because I don't have to scroll down to the last row every time I open the spreadsheet.
try it this way:
function onEdit(e) {
//e.source.toast('Entry');
var sh=e.range.getSheet();
if(sh.getName()!='Sheet Name')return;//you need to change this sheetnamee
var X=sh.getRange('C2').getValue();
if(X=='Past campaigns - actual cashflows to date only') {
//e.source.toast('flag1');
sh.hideRows(e.range.rowStart,e.range.rowEnd-e.range.rowStart+1);
}
}
I hope you're not trying to run this from the script editor because that's not going to work.

Can't find a setIndex in SpreadsheetApp

I want to code some protection into a Google Sheets spreadsheet, that will keep the pages in the right order. I can't protect the entire sheet from modification as it's a group workbook used to organize.. well the organization :P
In google scripts I found the getIndex but i need a setIndex
here is the skeleton of the code, in case I'm not being clear
function resetSheetPos() {
var s = SpreadsheetApp.getActiveSheet();
if s.getSheetName() = "Instructions" {
//the function I can't find would look like this
//s.setIndex(0);
s.activate(); // in case we aren't still looking at the same sheet when it changed it's index
};
if s.getSheetName() = "Notes and Comments" {
//and would be incremented for each sheet
//s.setIndex(1);
s.activate(); //again only if needed.
};
// and so on for each sheet
};
You'll need you use methods on the parent Spreadsheet rather than the Sheets directly.
See moveActiveSheet(index), or possibly insertSheet(index)
https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#moveActiveSheet(Integer)