Google apps脚本,onclick和href之间的优先级

问题描述 投票:1回答:1

我有一个按钮(在[[HTML中),该按钮都需要函数LoginInfoInput(...)并重定向到页面(href="..."),即:

<a onclick="LoginInfoInput('<?!= userID; ?>')" href="<?= ScriptApp.getService().getUrl(); ?>?v=form&userID=<?!= userID; ?>" class="waves-effect waves-light btn-large">Log In</a>

其中LoginInfoInput(...)是一个JavaScript函数,它将触发我的Google Apps脚本(GS)函数。然后,触发的GS函数将数据插入我的电子表格。同时,重定向的URL将通过我的doGet()函数。为了确定结果,它从我的电子表格中获取数据。我的问题是电子表格在重定向页面之前不总是及时更新。

我解决问题的尝试是使用while循环等待/暂停。但是,当while循环处于活动状态时,在此期间数据(至少在视觉上)在电子表格中可用,这是不起作用的,即,无法访问感兴趣的数据。注意,我还在while循环中添加了用于更新存储电子表格数据的变量的选项(调用SpreadsheetApp.openByUrl(url)),但是没有任何改进。

更新

我的LoginInfoInput()(在[[JavaScript中)如下:

function LoginInfoInput(userID){ var userLO = document.getElementById("id_loginLO").value; var userPassword = document.getElementById("id_password").value; google.script.run.LoginAdd(userLO, userPassword, userID); }

解决方案>>

鉴于onclickhref在我的问题中似乎是异步运行的,因此我可以借助@IMTheNachoMan see below的答案找到解决方案。我使用的解决方案是将上述代码部分替换为:

<a onclick="return LoginInfoInput(this, '<?!= userID; ?>');" href="javascript:void(0)" class="waves-effect waves-light btn-large">Log In</a> <param id="currURL" value="<?= ScriptApp.getService().getUrl(); ?>?v=form&amp;userID=<?!= userID; ?>">

function LoginInfoInput(a, userID)
{
    if(a.className == "done")
    {
        return true;
    }
    else
    {
        var userLO = document.getElementById("id_loginLO").value;
        var userPassword = document.getElementById("id_password").value;                     
        google.script.run.withSuccessHandler(function(){
            a.className = "done";
            a.href = document.getElementById("currURL").value;
            a.click();
        }).LoginAdd(userLO, userPassword, userID);
    }
}

我有一个按钮(使用HTML),该按钮既需要函数LoginInfoInput(...),又要重定向到页面(href =“ ...”),即:

url redirect google-apps-script priority-queue buttonclick
1个回答
1
投票
用此替换您的功能:

function LoginInfoInput(a, userID) { if(a.className == "done") { return true; } else { google.script.run.withSuccessHandler(function(){ a.className = "done"; a.click(); }).LoginAdd(); } }

[当用户单击链接时,className不是done,因此它将调用您的GAS函数并返回false,因此不会打开href。完成GAS功能后,它会设置className,然后再次设置点击次数。这次它返回true,因此href将运行。

这有意义吗?

注意,您也可以使用withUserObjecta传递URL。 

参考:https://developers.google.com/apps-script/guides/html/reference/run

© www.soinside.com 2019 - 2024. All rights reserved.