使用 gs 函数和 appscript 上 html 中的选择的输入

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

我正在尝试使用 html 提供的输入来使用 gs 函数。 该输入是通过使用 gs 函数 listWebinarSheets_i 创建列表来选择的,该函数有效。 我尝试了很多方法,但没有任何效果,当我

console.log(google.script.run)
时,我可以看到我的函数,但它没有被执行。我已将调用的函数简化为
console.log("1");
但控制台日志上仍然没有任何内容。

我的html:

<!DOCTYPE html>
<html>    
    <head>
    <base target="_top">
    <script>
        function copyParticipants() {
        const selectedSheet = document.getElementById('sheetSelect').value;  
        google.script.run<!DOCTYPE html>
<html>
    <head>
    <base target="_top">
    <script>
    function copyParticipants() {
        const selectedSheet = document.getElementById('sheetSelect').value;  
        google.script.run
            .withSuccessHandler(function() {
                alert('won : ' + selectedSheet);
            })
            .withFailureHandler(function(error) {
                alert('Erreur : ' + error.message);
            })
            .copytoto(selectedSheet);  
        }
    </script>
    </head>
    <body>
        <h3>Select a sheet :</h3>
    
    <!-- List -->
    <select id="sheetSelect">
        <? for (var i = 0; i < sheetNames.length; i++) { ?>
            <option value="<?= sheetNames[i] ?>"><?= sheetNames[i] ?></option>
        <? } ?>
    </select>
    
    <br><br>

    <button onclick="copyParticipants()">Copy Participants</button>


    </body>
</html>

我的GS:

function showInterface_i() {
    var sheetNames = listWebinarSheets_i();  // getting a list of names
    var template = HtmlService.createTemplateFromFile('interface');
    template.sheetNames = sheetNames;  
  
    const html = template.evaluate()
        .setWidth(400)
        .setHeight(300);
    SpreadsheetApp.getUi().showModalDialog(html, 'Interface');
}


function copytoto(sheetname){
    console.log("1");
}

我试图将控制台日志放在任何地方,在html上它按预期工作,但在使用google.script.run调用的函数上(所以在.gs中),没有。除了控制台日志之外,初始功能单独运行也很好。我尝试从模板化 html 中提出解决方案,但还没有任何效果。

google-sheets google-apps-script web-applications
1个回答
0
投票

无法使用Google.script.run执行

我已经运行了一些测试,发现您可能缺少与成功处理程序和失败处理程序配对的东西,因为如果精简到运行东西的基础知识,我就能够运行复制功能。我添加了一些代码来与错误处理程序和成功处理程序配对。我还添加了一些部分,以便我可以复制案例,请随意编辑它们,只要您认为合适。更正了 HTML 上的一些错误位置的标签。

示例代码

HTML:

<!DOCTYPE html>
<html>
<head>
    <base target="_top">
    <script>
        function copyTo() {
            const selectedSheet = document.getElementById('sheetSelect').value;  
            google.script.run
                .withSuccessHandler(function(message) {
                    alert('won : ' + message); 
                })
                .withFailureHandler(function(error) {
                    alert('Erreur : ' + error.message);
                })
                .copyData(selectedSheet); 
        }
    </script>
</head>
<body>
    <h3>Select a sheet:</h3>
    
    <!-- List -->
    <select id="sheetSelect">
        <? for (var i = 0; i < sheetNames.length; i++) { ?>
            <option value="<?= sheetNames[i] ?>"><?= sheetNames[i] ?></option>
        <? } ?>
    </select>
    
    <br><br>

    <button onclick="copyTo()">Copy Participants</button>
</body>
</html>

代码.gs

function showInterface_i() {
    var sheetNames = listWebinarSheets_i();  // Get a list of names
    var template = HtmlService.createTemplateFromFile('interface');
    template.sheetNames = sheetNames;  
  
    const html = template.evaluate()
        .setWidth(400)
        .setHeight(300);
    SpreadsheetApp.getUi().showModalDialog(html, 'Interface');
}

function copyData(sheetName) {
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
    if (!sheet) {
        throw new Error('Source sheet not found: ' + sheetName);
    }

    var data = sheet.getDataRange();
    var targetSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("TargetSheet");
    if (!targetSheet) {
        throw new Error('Target sheet not found: TargetSheet'); // Throwing an error for something to handle your onFailure
    }

    data.copyTo(targetSheet.getRange(1, 1), {contentsOnly: true});
    return 'Data copied from: ' + sheetName; // Returning a message on Success to match your HTML Google Script Run Requirements
}

function listWebinarSheets_i() { //Created a sudo list generation since it is not included on your post.
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheets = ss.getSheets();
    return sheets.map(x => x.getSheetName()); 
}

示例输出

Sample Output with Success

参考资料:

Google 脚本运行

注:

编辑后的代码有点慢,请耐心测试。 请随意询问有关代码的问题。请确保该表 必须修改脚本复制部分的名称以匹配您的 当前项目

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