代码自动执行,无需单击按钮

时间:2020-09-17 21:28:13

标签: google-apps-script

我从Khizer Mehmood修改了一段代码和HTML。很棒的代码,将包括链接。它几乎可以满足我的所有需求。但是它直接触发了地理位置,我想在另一列中添加信息,然后将其发送到表格中。

我不知道如何修改此HTML和代码,以便它会提示我添加信息,然后再将其全部发送到Google表格

这是我的代码:

JavaScript:

function doGet() {
    return HtmlService.createHtmlOutputFromFile('Index');
}

function getLoc(value) {
    var SpreadSheetKey = "1NDDhHomd4uJ8Xc2OlmK_jtShCKRLuHrwMBieURtM8zo";
    var ss = SpreadsheetApp.openById(SpreadSheetKey);
    var respSheet = ss.getSheets()[0];
    var data = respSheet.getDataRange().getValues();
    var headers = data[0];
    //var numColumns = headers.length ;
    var numResponses = data.length;
    var c = value[0];
    var d = value[1];
    var e = c + "," + d; {
        respSheet.getRange(numResponses + 1, 1).setValue(Utilities.formatDate(new Date(), "GMT+2", "MM/dd/yyyy HH:mm:ss"));
        respSheet.getRange(numResponses + 1, 2).setValue(e);
        var response = Maps.newGeocoder().reverseGeocode(value[0], value[1]);
        f = response.results[0].formatted_address;
        respSheet.getRange(numResponses + 1, 3).setValue(f);

    }
}

HTML:

<!DOCTYPE html>
<html>
   <script>
      (function getLocation() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition);
        }
      })()
      function showPosition(position) {
       var a= position.coords.latitude;
       var b= position.coords.longitude;
       var c=[a,b]
       getPos(c)
       function getPos(value) {
       google.script.run.getLoc(value);
       }
      }
   </script>
   <button onclick="myFeed">Copy Text</button>
   <body>
      <h2>Thank you!</h2>
      <p> Your registration is complete. Have a great day! </p>
      <img src="https://drive.google.com/thumbnail?id=1wheniE0HFtA37frkolHfmnjRLFbNkTI3" alt="W3Schools.com" style="width:100px;height:120px;">
   </body>
</html>

1 个答案:

答案 0 :(得分:0)

检查HTML内的JS。您正在使用"Immediately Invoked Function Expression"。通过将函数getLocation()括在括号中,可以告诉浏览器执行该表达式。因此,您可以立即完成此操作。

解决方案:

删除括号,并在单击按钮时调用该函数。

示例:

在HTML中添加以下按钮:

<button onclick="getLocation">Get Location</button>