Great video. How would I handle the fact that server functions don't seem to adhere to asynchronous principles. For instance if I fetch data from an external rest api and and write the new lines to a sheet with setValues, I am unable to read those new values straight after as it seems gas has its own way of bundling actions and optimizing so about 1 out of 10 run I get the new data, but the others the server functions finishes before I can access the newly written data. I've tried flush but it doesn't seem to do anything
Hello, thank you for the perfectly understandable video and example, this tutorial helps me with automation a lot of business processes with apps script. thank you very much.
bunch of thanks for the great video. I would like to ask. When you wrote a generic function using promise to be called when calling any function on the server. what if the function on the server has perameters? is there a way to still use a generic function using promise to have the ability to use Async, Await? hope you can see this, many thanks again. Firas
How can I possibly to pass an argument to getData using runGoogleScript? I believe something like below won't work: await runGOogleScript('getData(options)')
bunch of ways to do it You could pass as a second parameter await runGOogleScript('getData',options) and then in runGOogleScript google.script.run[func](options)
Nice tutorial, I was trying to run a server side function, and I was wondering how to run server side function from client side by passing some argument parameter from client side to server side.
I tried below code and it worked fine. Added params = [ ] function runGoogleScript(serverFunctionName, params = []) { const promise = new Promise((resolve, reject) => { google.script.run .withSuccessHandler((data) => { resolve(data); }) .withFailureHandler((er) => { reject(er); }) [serverFunctionName](...params); }); return promise; } Call the server side function with const result = runGoogleScript('myServerSideFunction', [param1, param 2 ]); I think we can also use params as an object but it needs more code on server side.
@@getitdonetube Thanks a lot. I tried that and it is working. But I am facing a small problem. The function (it generates a pdf from user input and sends email with that pdf) takes additional 10seconds as the function is somehwhat big and as a result the browsers remain busy during that time. is there any alternative for the same as both the process can be done in the background and doesnt't require anything from client side.
@@getitdonetube I tried but I realized that triggers don't support parameters but I need to somehow pass value to the function as the pdf will be generated based on the response of the doGet. Is there any way to fix my issue?
Here is a wrapper by Steven de Salas that stores arguments in script properties and then executes the trigger using those. gist.github.com/sdesalas/2972f8647897d5481fd8e01f03122805
It's up to you, there are many different ways. One simple way is to have a second parameter in your function and pass it to google.script.run[func](param2)
@@getitdonetube Thanks for the helpful tutorial. I had the same question, but how to get it to process MULTIPLE parameters? It seems to treat param2 as a single string when defining const param2 = "param2a, param2b"
I've been trying to understand how to use promises in apps script. I'm using a getPost function to get a value on a web app however I keep getting the same error no matter what I do: Uncaught (in promise) SyntaxError: Unexpected end of input at userCodeAppPanel:38:17 my getPost function references this function called test1 ("return test1()") function test1(){ let promise1 = test2("hello"); let promise2 = test2("world") Promise.all([promise1,promise2]).then((values)=>{ Logger.log(values); return ContentService.createTextOutput("hello").setMimeType(ContentService.MimeType.JSON); }); } function test2(textToPass){ return new Promise((resolve,reject)=>{ resolve(textToPass); }) }
I've been getting the following error when I call the following doPost function after it is deployed as a web app from sheets. I am using the fetch api to call a POST method as you have described. Here is my doPost function . What throws the error is the fact that I am trying to get the value of the resolved promise. POST net::ERR_FAILED 200 doStuff @ userCodeAppPanel:19 userCodeAppPanel:19 Uncaught (in promise) TypeError: Failed to fetch at HTMLButtonElement.doStuff (userCodeAppPanel:19:9) function doPost(e){ let promiseResponse = new Promise((resolve,reject)=>{ resolve("hello"); }); promiseResponse.then(value =>{ const JSONaneeka = [{status: value}]; return ContentService.createTextOutput(JSON.stringify(JSONaneeka)).setMimeType(ContentService.MimeType.JSON); }) }
after 2 days of suffering, i finally found your tutorial sir, you are absolutely my life saver
Probably the first tutorial on Promises that actually starts to make sense to me!
Wow, your App Script tutorials are the best I've found on the entire internet!! Thank you so so much, you've saved me so many hours of time.
to be able to call google script run multiple functions at once, that's very useful. I didn't know, I was only doing one big function.
dude you are a literal life saver
Great video. How would I handle the fact that server functions don't seem to adhere to asynchronous principles. For instance if I fetch data from an external rest api and and write the new lines to a sheet with setValues, I am unable to read those new values straight after as it seems gas has its own way of bundling actions and optimizing so about 1 out of 10 run I get the new data, but the others the server functions finishes before I can access the newly written data. I've tried flush but it doesn't seem to do anything
Hello, thank you for the perfectly understandable video and example, this tutorial helps me with automation a lot of business processes with apps script. thank you very much.
Great to hear!
Your tutorials are so helpful. Thx
Great video that helps a lot! Thanks for all your effort 👍
bunch of thanks for the great video.
I would like to ask. When you wrote a generic function using promise to be called when calling any function on the server.
what if the function on the server has perameters?
is there a way to still use a generic function using promise to have the ability to use Async, Await?
hope you can see this, many thanks again.
Firas
Thank you Sir,
I need it, But Your Code is Promising.
Thankyou🙏
How can I possibly to pass an argument to getData using runGoogleScript?
I believe something like below won't work:
await runGOogleScript('getData(options)')
bunch of ways to do it
You could pass as a second parameter
await runGOogleScript('getData',options)
and then in runGOogleScript
google.script.run[func](options)
In this script how i send a value to server
Awesome.
If I get you right: it's a "cleaner" way for multiple backend-requests, right?
Thank you 😊
Yes.
When to share new video???
Loving the new videos
Thanks!
Nice tutorial, I was trying to run a server side function, and I was wondering how to run server side function from client side by passing some argument parameter from client side to server side.
I tried below code and it worked fine. Added params = [ ]
function runGoogleScript(serverFunctionName, params = []) {
const promise = new Promise((resolve, reject) => {
google.script.run
.withSuccessHandler((data) => {
resolve(data);
})
.withFailureHandler((er) => {
reject(er);
})
[serverFunctionName](...params);
});
return promise;
}
Call the server side function with
const result = runGoogleScript('myServerSideFunction', [param1, param 2 ]);
I think we can also use params as an object but it needs more code on server side.
@@abdullahkasapoglu6526 thanks , I will try it
It is a perfected video!
Hi, how to execute a task after executing a doPost triggered from the client side ? Please help me with it
just add the function in doPost?
@@getitdonetube Thanks a lot. I tried that and it is working. But I am facing a small problem. The function (it generates a pdf from user input and sends email with that pdf) takes additional 10seconds as the function is somehwhat big and as a result the browsers remain busy during that time. is there any alternative for the same as both the process can be done in the background and doesnt't require anything from client side.
try
ScriptApp.newTrigger("createPDFfunction")
.timeBased()
.after(1)
.create();
@@getitdonetube I tried but I realized that triggers don't support parameters but I need to somehow pass value to the function as the pdf will be generated based on the response of the doGet. Is there any way to fix my issue?
Here is a wrapper by Steven de Salas that stores arguments in script properties and then executes the trigger using those.
gist.github.com/sdesalas/2972f8647897d5481fd8e01f03122805
In this script how i send a value to server, how to send parameter to GS, i saw this question with no aswer
It's up to you, there are many different ways.
One simple way is to have a second parameter in your function and pass it to google.script.run[func](param2)
@@getitdonetube works fine, thnaks... you are the best
@@getitdonetube Thanks for the helpful tutorial. I had the same question, but how to get it to process MULTIPLE parameters? It seems to treat param2 as a single string when defining const param2 = "param2a, param2b"
I've been trying to understand how to use promises in apps script. I'm using a getPost function to get a value on a web app however I keep getting the same error no matter what I do: Uncaught (in promise) SyntaxError: Unexpected end of input
at userCodeAppPanel:38:17
my getPost function references this function called test1 ("return test1()")
function test1(){
let promise1 = test2("hello");
let promise2 = test2("world")
Promise.all([promise1,promise2]).then((values)=>{
Logger.log(values);
return ContentService.createTextOutput("hello").setMimeType(ContentService.MimeType.JSON);
});
}
function test2(textToPass){
return new Promise((resolve,reject)=>{
resolve(textToPass);
})
}
what I meant is my doPost(e) function calls "return test1()"
I've been getting the following error when I call the following doPost function after it is deployed as a web app from sheets. I am using the fetch api to call a POST method as you have described. Here is my doPost function . What throws the error is the fact that I am trying to get the value of the resolved promise.
POST net::ERR_FAILED 200
doStuff @ userCodeAppPanel:19
userCodeAppPanel:19 Uncaught (in promise) TypeError: Failed to fetch
at HTMLButtonElement.doStuff (userCodeAppPanel:19:9)
function doPost(e){
let promiseResponse = new Promise((resolve,reject)=>{
resolve("hello");
});
promiseResponse.then(value =>{
const JSONaneeka = [{status: value}];
return ContentService.createTextOutput(JSON.stringify(JSONaneeka)).setMimeType(ContentService.MimeType.JSON);
})
}
Google's Apps Script service runs synchronous code only, at least for now, so I don't believe you are allowed to use promises on the server side.
That makes so much sense! Thanks for letting me know. I would have kept trying forever otherwise.