Calling a bound script from a different container - google-apps-script

Essentially I want to use a bound script in a Google Form to call a function in a Google Sheet that will go and edit yet another sheet. Ideally I could eliminate those steps if the onEdit trigger in the data sheet would get called when a new form submission happens, but that isn't working so now we're here. I really just need the syntax to know how to trigger a bound function in the Sheet's script by running the onFormSubmission trigger that is bound to a Google Form.
I haven't really tried much, I think all I need is to be told that it is impossible, OR HOPEFULLY just to see what the proper syntax is. I'm new to Google Apps Script, I think I'm like 2 hours in or so.
function onFormSubmit() {
//call the populateJournal() function that is bound to the Sheet
}
After I get this done I'm gonna need to have the populateJournal() function edit a sheet that is another container entirely, and I'm guessing that syntax is gonna look marginally similar in terms of referencing an external container. Any help would be greatly appreciated, my apologies for my ineptitude.

If you are able to send the form responses to the spreadsheet by using the built-in feature for that, instead of using the on form submit event of the bounded to form script uses the on form submit event of the bounded to spreadsheet script.

Related

Google Apps Script: How are edit events from a bounded script in one sheet treated on another?

I'll try to be as specific as possible. This questions relates to Google Apps Script, bounded to two sheets.
I have a function that runs on edit through an installable (not simple) trigger. (Installable because it needs to call e.g. GmailApp). Call it Sheet A.
On another sheet, which we'll call Sheet B, is a script which causes an edit to be made in a cell on Sheet A.
When an edit is made manually on Sheet A, the installable on-edit script runs fine.
When the edit is made via the script on Sheet B, the installable on-edit script does not run. In other words, there doesn't seem to be any event associated with edits made by a script.
I am wondering if there is any way to trigger the on-edit script on Sheet A to run when changes are being made through another, external, bounded script.
I've looked through GAS documentation and searched as best I can here, but I cannot find an answer. I've tried to use a simple trigger, but it also does not seem to "pick up" the edit event when performed by another script.
Any help would be greatly appreciated.
Triggers only run on "human" changes. You need to rethink your flow.
If you have a function funA() changing something programmatically in a cell and you need to trigger another function funB(), just call funB() directly from funA() without counting on a trigger.
onEdit is triggered only if user updates a cell, not when the cell is updated programmatically.
You should expose onEdit from sheet A (or extract the method that actually does send the emails) and publish it as API executable
Then you can call that method from sheet B, after the edit is made programmatically.

Custom google sheets function that auto updates when change is made wont change when others edit

I have a custom function that auto updates when a change is made on the google spreadsheet, but when some other users edits the sheet the function doesn't work for them, how can i make it update for anyone that uses the sheet.
The function auto updates using this:
function onEdit(e) {
SpreadsheetApp.getActiveSheet().getRange('Z1').setValue(Math.random());
}
Then passing the parameter of Z1.
Explanation:
Just a couple of notes to make sure we are on the same page.
This is not a custom function but an onEdit trigger function.
As the name suggests it is triggered when user changes the value of any cell in a spreadsheet.
The function is only triggered when the change of the cell is made by a user and not by formulas or scripts.
Potential Issues:
Make sure the other users have edit permissions on the target cell. If they are not allowed to edit cell Z1 in the activeSheet which might be any sheet in the spreadsheet file, then the script won't work for them. You can either give them access, or make the script work only under particular sheets when you can give them access.
Some users sometimes have experienced issues when tried to chain with active methods directly. See reference links here and here although I can't reproduce that issue, it might be the cause in your case.
Potential Solution:
Please take care of the first issue.
Regarding the second issue, modify your code as follows:
function onEdit(e) {
const active_sheet = e.range.getSheet();
active_sheet.getRange('Z1').setValue(Math.random());
}
This is anyway the recommended way to use an onEdit trigger as you should take advantage of the event object.

Copy cell background to other spreadsheets

I am new to Google Apps Script and I don't know how to do some codes. I have two spreadsheets, 1 for the admin and 1 for the client. Every time the admin will update the cells the client side must be updated also. However, only the data in the cells are copied but not the backgrounds in it. Here are the screenshots for better understanding:
Copy value and background from Admin to Client
This requires an installable onEdit(e) trigger. You will also need the id of the client spreadsheet. Note you cannot run this from the Script Editor unless you do it from another function that supplies the event object. See this example.
function updateClient(e) {
var ss=SpreadsheetApp.openById('ClientSideSpreadsheetId');//You need to provide id here. You cannot pass other parameters to this function because it a trigger.
var sh=ss.getSheetByName(e.range.getSheet().getName());
var rg=sh.getRange(e.range.rowStart,e.range.columnStart);
rg.setValue(e.value);
rg.setBackground(e.range.getBackground());
}
I tested this on my account and it works. I suspect that you'll have trouble getting it to run so read the answer in the link I provided thoroughly before telling me that it doesn't work.
This is what the event object looks like:
{"authMode":{},"range":{"columnStart":3,"rowStart":18,"rowEnd":18,"columnEnd":3},"source":{},"user":{"nickname":"nickname","email":"email#email.com"},"triggerUid":"123456","value":"3"}
Event Objects

On cell selection

I am learning to use google app script. I have written code to get row number in which so far I succeeded but can't find a way to assign it to a cell or row change event trigger. Can someone help me figure out how to do this?
My code is as follows :
function onChange(event) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var currentCell = sheet.getCurrentCell();
sheet.getRange("B1").setValue(currentCell.getRow());
}
There are no "onSelect" trigger on Google Apps Script but you could use the poll technique. Basically, you will have to use a sidebar to hold client-side code that will check for the selected cell every determined time. For code examples checkout the following Q/A
How do I make a Sidebar display values from cells?
How to detect user changing sheet?
Have you installed the trigger? onChange triggers need to be explicitly hooked up because they're considered installable triggers (opposed to simple triggers, which just require saving the .gs file).
You can manage your triggers from the Menu in your Apps Script Editor:

Google App Script onFromSubmit Trigger doesn't trigger

I have a spreedsheet, with a sheet feed by a Google Form. unfortunetly, I need special features, like conditional formatting, and data added by google form are added in a new line without format.
So, I did an other sheet, with the same columns, and I had to copy paste from the first one to the second one. I'd like this copy to be automated. So I tried to do it using Google App Script. But it seems like I did something wrong with the trigger.
Here is the code :
function creerTrigger()
{
ScriptApp.newTrigger('deplacerCommande').forSpreadsheet("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").onFormSubmit().create();
}
function deplacerCommande(e)
{
var id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var feuille = SpreadsheetApp.openById(id).getSheetByName("Commande");
feuille.appendRow(e.namedValues);
}
When I run deplacerCommande() from Google App Script, the messageBox is displayed, but not when a form is submitted, so I don't think the trigger works.
Yes, I created my trigger. I did it by running the function creerTrigger() from Google App Script. I also tried deleting it, then changing the version, then recreating it, because I read it can solve the problem, but it didn't.
list of trigger
It's in French, but I'll translate the value for you :
deplacerCommande is the name of the function called by the trigger.
the second value means From the spreedsheet.
the third means sending Form
The solution was simple. The trigger triggered, but the appendRow function raised an Exception because namedValue is an object, not an arrow. using e.values instead of e.namedvalues solved the problem!