google.script.run replaced with JavaScript Promise API, Async & Await in a Web App

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ม.ค. 2022
  • Learn how to use JavaScript Promise API with async & await keywords in your Web App instead of google.script.run
    Previous part • google.script.run Web ...
    #promise #async #await

ความคิดเห็น • 40

  • @pgd724
    @pgd724 2 ปีที่แล้ว +4

    Probably the first tutorial on Promises that actually starts to make sense to me!

  • @lunaRdeltaY
    @lunaRdeltaY 9 หลายเดือนก่อน +1

    after 2 days of suffering, i finally found your tutorial sir, you are absolutely my life saver

  • @mistersir
    @mistersir 2 ปีที่แล้ว +4

    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.

  • @zummon
    @zummon 11 หลายเดือนก่อน +1

    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.

  • @bradlegassick9327
    @bradlegassick9327 2 ปีที่แล้ว

    Your tutorials are so helpful. Thx

  • @jesche6240
    @jesche6240 ปีที่แล้ว

    Great video that helps a lot! Thanks for all your effort 👍

  • @sudiipkumarbasu4194
    @sudiipkumarbasu4194 2 ปีที่แล้ว

    Thank you Sir,
    I need it, But Your Code is Promising.
    Thankyou🙏

  • @mathieutermote1967
    @mathieutermote1967 2 ปีที่แล้ว

    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.

  • @hvhryan2888
    @hvhryan2888 2 ปีที่แล้ว

    It is a perfected video!

  • @Fshhady
    @Fshhady ปีที่แล้ว +1

    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

  • @lpanebr
    @lpanebr 2 ปีที่แล้ว

    Loving the new videos

  • @SaintHanappi
    @SaintHanappi 2 ปีที่แล้ว +1

    Awesome.
    If I get you right: it's a "cleaner" way for multiple backend-requests, right?
    Thank you 😊

  • @RRCaddick
    @RRCaddick 7 หลายเดือนก่อน

    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

  • @avantiya
    @avantiya 2 ปีที่แล้ว

    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.

    • @abdullahkasapoglu6526
      @abdullahkasapoglu6526 2 ปีที่แล้ว +3

      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.

    • @avantiya
      @avantiya 2 ปีที่แล้ว

      @@abdullahkasapoglu6526 thanks , I will try it

  • @sudiipkumarbasu4194
    @sudiipkumarbasu4194 2 ปีที่แล้ว +2

    In this script how i send a value to server

  • @gaaraxpo
    @gaaraxpo ปีที่แล้ว +1

    How can I possibly to pass an argument to getData using runGoogleScript?
    I believe something like below won't work:
    await runGOogleScript('getData(options)')

    • @getitdonetube
      @getitdonetube  ปีที่แล้ว +1

      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)

  • @HimanshuGogoi
    @HimanshuGogoi 2 ปีที่แล้ว

    Hi, how to execute a task after executing a doPost triggered from the client side ? Please help me with it

    • @getitdonetube
      @getitdonetube  2 ปีที่แล้ว

      just add the function in doPost?

    • @HimanshuGogoi
      @HimanshuGogoi 2 ปีที่แล้ว

      @@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
      @getitdonetube  2 ปีที่แล้ว +1

      try
      ScriptApp.newTrigger("createPDFfunction")
      .timeBased()
      .after(1)
      .create();

    • @HimanshuGogoi
      @HimanshuGogoi 2 ปีที่แล้ว

      @@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?

    • @getitdonetube
      @getitdonetube  2 ปีที่แล้ว +1

      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

  • @galaxybcbc7007
    @galaxybcbc7007 ปีที่แล้ว

    In this script how i send a value to server, how to send parameter to GS, i saw this question with no aswer

    • @getitdonetube
      @getitdonetube  ปีที่แล้ว

      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)

    • @galaxybcbc7007
      @galaxybcbc7007 ปีที่แล้ว

      @@getitdonetube works fine, thnaks... you are the best

    • @ademoor
      @ademoor ปีที่แล้ว

      @@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"

  • @georgejacob9891
    @georgejacob9891 ปีที่แล้ว

    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);
    })
    }

    • @georgejacob9891
      @georgejacob9891 ปีที่แล้ว

      what I meant is my doPost(e) function calls "return test1()"

  • @georgejacob9891
    @georgejacob9891 ปีที่แล้ว

    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);
    })
    }

    • @getitdonetube
      @getitdonetube  ปีที่แล้ว

      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.

    • @georgejacob9891
      @georgejacob9891 ปีที่แล้ว

      That makes so much sense! Thanks for letting me know. I would have kept trying forever otherwise.