google脚本WebApp从URL获取参数

时间:2020-08-26 19:19:42

标签: javascript google-apps-script web-applications

我有一个带有链接的google脚本WebApp

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

在用户方面-用HTML代码-我可以获得“ v”值吗? 我试图搜索类似的问题并开发了此

 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)

但返回错误“未定义ScriptApp”

这里有解决方案吗?

谢谢

2 个答案:

答案 0 :(得分:0)

ScriptApp这样的服务器端库在客户端不可用。

要在客户端获取url查询参数和哈希,请使用google.script.url.getLocation

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

答案 1 :(得分:0)

要在客户端上运行自定义应用脚本,您必须使用:

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

根据您的情况:

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