I have a sheet that when the onchange triggers it makes a mirror of the information to use as a reference later when it determine which rows changed. I had this connected to a Query function. Whenever the query got updated, the onChange function would run. But sometimes the query would act weird and information would appear blank on the sheet it copies to even though i can see it on the query. And if two computers were looking at the query at the same time, it was evident that it didn't update on all screens. So i decided, why not, instead of a query, we have the information get updated the same way the mirror is updated. So it copies all the information down and puts it onto the sheet, no query needed. I figured this would at least make sure everyone is seeing the same information. So i made a query sheet, and added an onChange() trigger to it that grabbed all the info and pasted it onto the main sheet, whenever the query updated. (even if it messes up, at least it's much more obvious and no one is seeing anything different). But when the information was posted onto the main spreadsheet. THAT spreadsheet's onChange function didn't trigger. I thought it would trigger whenever there was a change. No matter what. So I then decided to create a new function in the Query Sheet where it inserts and deletes the last row, even say hi, thinking if I make the sheet change more prominently, the onChange() function would trigger. Still didn't. I'm at a loss.
function onChangeMirror() {
var sos = SpreadsheetApp.openById("the sheet with the query");
var sas = SpreadsheetApp.setActiveSpreadsheet(sos);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s1 = ss.getActiveSheet();
var lastRow = s1.getLastRow();
var lastRow1 = s1.getLastRow()+1;
var lastRow10 = s1.getLastRow() +10;
var SRange = s1.getRange(2,1,lastRow,2);
var A1Range = SRange.getA1Notation();
var SData = SRange.getValues();
var sos2 = SpreadsheetApp.openById("the sheet it's updating");
var sas2 = SpreadsheetApp.setActiveSpreadsheet(sos2);
var ss2 = SpreadsheetApp.getActiveSpreadsheet();
var s2 = ss2.getActiveSheet();
var MaxRow = s2.getMaxRows();
s2.getRange(A1Range).setValues(SData);
Logger.log(lastRow1)
Logger.log(lastRow10)
s2.getRange('A'+ lastRow1+':'+'B'+lastRow10).setValue(null);
Logger.log(A1Range)
if(MaxRow > '300') {
s2.deleteRow(MaxRow)
}
else if(MaxRow < '301'){
s2.insertRowAfter(MaxRow)
s2.getRange('A'+(MaxRow+1)).setValue('HI')
}
}
here is the script i was using
Related
I have a Google sheet that automatically changes the urls in cell A28 via another script. The "test()" script is called by the main script to be executed whenever the url changes. I used the code from another thread: Grab data from website HTML table and transfer to Google Sheets using App-Script.
I modified it a bit because what interested me was getting the table with the ID "pgl_basic" and actually the code works as I expected. The only problem is that, in an absolutely random way, sometimes this error is generated: "Cannot read properties of null [0]" and if I then restart the script everything is fine or it crashes before or after the url it has determined the error, therefore it is absolutely random and without an apparent correlation.
function test() {
var tableHtml = 0;
var sheet = SpreadsheetApp.getActive().getSheetByName("2023");
var url = sheet.getRange("A27").getValue();
var html = UrlFetchApp.fetch(url, {muteHttpExceptions: true}).getContentText().replace(/(\r\n|\n|\r|\t| )/gm,"");
var tableHtml = html.match(/<table\s.*?id="pgl_basic"[\s\S]*?<\/table>/)[0];
var trs = [...tableHtml.matchAll(/<tr[\s\S\w]+?<\/tr>/g)];
var data = [];
for (var i=0;i<trs.length;i++){
var tds = [...trs[i][0].matchAll(/<(td|th)[\s\S\w]+?<\/(td|th)>/g)];
var prov = [];
for (var j=0;j<tds.length;j++){
donnee=tds[j][0].match(/(?<=\>).*(?=\<\/)/g)[0];
prov.push(stripTags(donnee));
}
data.push(prov);
}
return(data);
}
function stripTags(body) {
var regex = /(<([^>]+)>)/ig;
return body.replace(regex,"");
}
Initially the line:
var tableHtml = 0;
It didn't exist in the original code and I added it thinking it was a tableHtml variable "cache" issue but that didn't fix my problems. Using the code of the aforementioned topic, however, there is no problem or error but that code fetches all the tables included in the website and since they are not all the same, I don't need it but I need that specific table with that specific ID. I also tried to use a simple 2 second delay (via Utilities.sleep) thinking it could be a connection loading problem but this doesn't seem to be the case, also because I repeat that the original code (even with much larger data) works without a hitch. What could be the solution? Thanks.
I am trying to automatically hide a sheet called "Add expense" when I am not active on it.
For example.
I have the sheet "BD Expenses", the sheet "BD Tokens", the sheet "BD Income", the sheet "Add expense", the sheet "Add income".
What I want is that when I am active in any of the sheets that is not called "Enter expense", then the script runs completely automatically (without having to click on any button) to hide the sheet called "Add expense".
I currently have this code:
//Ocultar todas las hojas excepto la activa
function Ocultar_formulario_ANADIR_GASTO() {
var sheets = SpreadsheetApp.getActiveSheet();
sheets.forEach(function(sheet) {
if (sheet.getSheetByName('Añadir gasto') != SpreadsheetApp.getActiveSheet().getName())
sheet.hideSheet();
});
};
I have tried various methods but without success.
I know there is a function called onSelectionChange (e) but since I am so new to Javascript I don't really know how to make it work. Hence I have created my code differently.
I have looked at the reference from https://developers.google.com/apps-script/reference/spreadsheet/sheet#hidesheet
I've also googled and stackoverflow, but can't find a solution to this problem.
Currently when running the script from the editor, I get the error "TypeError: sheets.forEach is not a function".
I can't get it to work.
I would really appreciate if someone can take a look at my code and offer me a little help.
Thank you very much.
It looks like there is no specific Google Scripts event for when you change sheets.
But there is a workaround mentioned in the issue tracker ticket - scroll to the bottom of the ticket to see it. It involves using onSelectionChange(e) to track which sheets you moved out of and into.
The following code adapts that workaround to your case:
The code assumes your Google spreadsheet has 2 or more sheets - and one of those sheets is called Secret Sheet. You can change this name to whatever you want (see the first line of the code).
When you move from the Secret Sheet to any other sheet, the Secret Sheet will automatically be hidden.
var secretSheetName = 'Secret Sheet'; // change this to whatever you prefer.
var prevSheetProperty = 'PREVIOUS_SHEET';
function saveActiveSheet() {
var activesheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var userProperties = PropertiesService.getUserProperties();
userProperties.setProperty(prevSheetProperty, activesheet.getSheetName());
}
function onSheetChange(e) {
var sheetToHide = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(secretSheetName);
sheetToHide.hideSheet();
}
function onSelectionChange(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Get current sheet name and compare to previously saved sheet
var currentSheetName = ss.getActiveSheet().getSheetName();
var userProperties = PropertiesService.getUserProperties();
var previousSheetName = userProperties.getProperty(prevSheetProperty);
if (currentSheetName !== previousSheetName) {
saveActiveSheet(); // this becomes the new "previous sheet".
if (previousSheetName === secretSheetName) {
// you have moved out of the secret sheet - so, hide it:
onSheetChange(e); // Call custom sheet change trigger
}
}
}
function onOpen(e) {
saveActiveSheet();
}
The script works by keeping track of which sheet is the currently active sheet - and which was the (different) previously active sheet before that.
It stores the "previous" sheet name in a user property.
You will see that to use the onSelectionChange(e) event, you simply have to add that function to your script:
function onSelectionChange(e) { ... }
Google Scripts automatically recognizes this as an event function. You can read more about this, with examples, here.
I am going to reply to my own question to provide the same solution as #andrewjames but for multiple sheets to be hidden at once.
The code to hide a single sheet can be found in #andrewjames comment.
The code to hide multiple sheets using the code of #adrewjames as a base, is this.
var addFicha = 'Añadir ficha'; // change this to whatever you prefer.
var addGasto = 'Añadir gasto'; // change this to whatever you prefer.
var addIngreso = 'Añadir ingreso'; // change this to whatever you prefer.
var hideMultipleSheets = addFicha || addGasto || addIngreso;
var prevSheetProperty = 'PREVIOUS_SHEET';
function saveActiveSheet() {
var activesheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var userProperties = PropertiesService.getUserProperties();
userProperties.setProperty(prevSheetProperty, activesheet.getSheetName());
}
function onSheetChange(e) {
var sheetToHide1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(addFicha);
var sheetToHide2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(addGasto);
var sheetToHide3 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(addIngreso);
sheetToHide1.hideSheet();
sheetToHide2.hideSheet();
sheetToHide3.hideSheet();
}
function onSelectionChange(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Get current sheet name and compare to previously saved sheet
var currentSheetName = ss.getActiveSheet().getSheetName();
var userProperties = PropertiesService.getUserProperties();
var previousSheetName = userProperties.getProperty(prevSheetProperty);
if (currentSheetName !== previousSheetName) {
saveActiveSheet(); // this becomes the new "previous sheet".
if (previousSheetName === hideMultipleSheets) {
// you have moved out of the secret sheet - so, hide it:
onSheetChange(e); // Call custom sheet change trigger
}
}
}
function onOpen(e) {
saveActiveSheet();
}
Here I leave a brief explanation so that if someone needs this code they know how to modify it so that it works for their needs.
1. At the beginning of all of the code, we see the first 3 vars.
In this case we want to hide 3 sheets, that's why there are only 3 vars. In case you want to hide 5 sheets, you should have 5 vars.
Here you can name them whatever you want. Then, the text in quotation marks is the name of the sheet you want to hide, so you must change it to the name of your sheets.
Example:
var hideSheettt1 = 'Here you need to copy the name of your sheet';
2. Following the first 3 vars, we have the 4 var hideMultipleSheets
In this case we are joining the first 3 vars into a single var to be able to reuse it later in the onSelectionChange (e) function
Here you must replace addFicha addGasto addIngreso with the name you have given to your first vars.
Continuing with the example from point 1, we will use the var hideSheettt1 = 'Here you need to copy the name of your sheet'; as a reference.
Example:
var hideMultipleSheets = hideSheettt1 || hideSheettt2|| hideSheettt3;
3. In the SheetChange (e) function you must substitute the var of .getSheetByName () in the first 3 vars, by the name of the var that you created at the beginning of the entire code (point 1).
Example:
var sheetToHide1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(hideSheettt1);
At this point, you should include as many vars as sheets you want to hide and vars you have created at the beginning of your code. In this case we want to hide 3 sheets. But if we wanted to hide 5 sheets, we must have 5 vars, one for each sheet.
We must also have the same amount of hideSheet()
I have two google separate forms that submit new client data to two separate spreadsheets. How would I use the onFormSubmit() on two separate spreadsheet sources in one app script editor? thank you all very much :D
if this is getting one sheet how could it implement two or more?
i think i get SpreadsheetApp.getSheetByName('xyz');
and im a little unsure exactly what an active sheet is or activated I think it is the sheet you're currently on in the spreadsheet the dev app script is being derived from. I might be missing some other basic concepts
function onFormSubmit(){
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sht = ss.getSheetByName('New Client Submission Form (Phone)'&&'New Client Submission Form (Emailed)')
var activeRng = sht.getRange("A2:K2")
var values = activeRng.getValues()
var height = values.length
var width = values[0].length
var ss_dest = SpreadsheetApp.openById('1wgzUSXFNLFQz6tv42Cp_o94qRPAB9IkZnKP5tGL003o')
var sht_dest = ss_dest.getSheetByName('Estimate Compiler')
var destRange = sht_dest.getRange(2,1,height,width)
destRange.setValues(values)
}
this function does exatly as expected I just don't know how to implement two or more yet
function onFormSubmit(e) {
var formObj={LinkedSheet1:FormName1,LinkedSheet2:FormName2};//you provide this from a knowledge of the connection between the form and linked sheet
var form=formObj[e.range.getSheet().getName()];//this returns the form for each sheet
switch (form) {
case 'FormName1':
//code for FormName1
break;
case 'FormName2':
//code for FormName2
break;
}
}
onFormSubmit Event Object
In my google spreadsheet i created a script (below) which give me the function/formula to get the name of the sheet/tab which i am currently in.
The problem is when i change the sheet name to a new name then this cell is not updating with the new name even after several refresh.
I have set triggers to run the script as well as i have used SpreadsheetApp.flush() to forcefully applies all pending Spreadsheet changes, but still this does not work.
This is my script below:
function sheetname() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveRange().getSheet();
return s.getName();
}
And this is with the flush :
function sheetname() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveRange().getSheet();
SpreadsheetApp.flush();
return s.getName();
}
Please let me know what changes i need to make so that this can update as and when the sheet name is changed.
This function works when connected to an onChange trigger and the Sheet Name is changed. I think you were adding it to the cell as "=sheetName()". I have to admit I don't know much about custom functions because I always do all of my calculations in arrays because it's much faster. But if you hook this up to an onChange event and set the load value of the sheet name into a cell it works.
function sheetName()
{
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sht=ss.getActiveSheet();
var name=sht.getName();
sht.getRange('A5').setValue(name);
}
I am trying to figure out how to remove someone from a spreadsheet using google app scripts.
Essentially, I have a central spreadsheet with information such as emails, names, UID's etc on it. I am trying to pull the email from this spreadsheet and use the removeEditor function to remove that email from another spreadsheet. You can view the code below.
var officerIDrow = officerID + 1;
var Tracker = SpreadsheetApp.getActive().getSheetByName('P_Tracker'); //the button is on the same sheet as this, this is why it is get active.
var PTBooking = SpreadsheetApp.openById("1JPS69ko99dQTEwjplF_l2l2G_T8RyHjxCyMxXd_AV_s"); //Referring to the other sheet where the email will be removed from editors.
var EmailAddressRange = "F" + officerIDrow; //This is the column where the emails are stored. The officer ID row refers to the user inputted value in a dialog. Don't worry about this, I have already checked to see if this is the issue,
var EmailAddress1 = Tracker.getRange(EmailAddressRange);
var EmailAddress2 = EmailAddress1.getValue();
var EmailAddress3 = Utilities.formatString(EmailAddress2); //In my desperation, I was trying to see if setting it as string would help.
PTBooking.removeEditor(EmailAddress3);
This isn't the full version of the code. If you'd like to see it, you can click here. But all that matters is above, everything else works fine.
The code above runs fine until it hits the last line where it tries to remove the email. The error message that appears says: "Invalid email: ". I'm assuming after the "email:" bit, it should display what has been pulled. But it doesn't! I have no idea why it isn't finding and using the email to remove people.
Can anyone spot any issues?
Thanks,
Shaun.
This basic test worked for me. It could read email address from the spreadsheet then remove editors from the external sheet. I tried to copy your example, but I don't know how the officerIDrow works, but I assume it's a number. So just stored a number as a var
function myTest() {
var ss = SpreadsheetApp.openById("ID HERE");
var idRow = 11;
var tracker = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("tracker");
var emailCell = "F"+idRow;
Logger.log(emailCell);
var emailAddress = tracker.getRange(emailCell).getValue();
Logger.log(emailAddress);
ss.removeEditor(emailAddress);
var editors = ss.getEditors();
Logger.log(editors);
}
I've left the Logger bits in as it will help point you at what script is doing at each point.
The email address in the cell must be plain, such as google#google.com