简体   繁体   中英

google script WebApp get parameter from URL

I have a google script WebApp with the link for example

https://script.google.com/a/macros/domain.com/s/******/dev?v=repDetails

On the users side - in HTML code - can I get the "v" value? I've tried to search similar issues and developed this

 var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = ScriptApp.getService().getUrl()
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
        }
    }
};

var id = getUrlParameter('v');
alert(id)

but it returns an error "ScriptApp is not defined"

Any solution here?

Thanks

Server side libraries like ScriptApp is not available client side.

To get the url query parameters and hash on the client side, use google.script.url.getLocation :

google.script.url.getLocation(location=>console.log(location.parameter))

To run a custom App Script on the client side you have to use:

google.script.run.withSuccessHandler(
      (result) => {
    //do something with the result
    }).withFailureHandler(
      (error) => {
    //do something with the error
      }
    ).nameOfYourFunction(params)

on your case:

google.script.run.withSuccessHandler(
      (params) => {
    //do something with the result
    }).withFailureHandler(
      (error) => {
    //do something with the error
      }
    ).getUrlParameter(sParam)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM