将数据一起传递到电子表格应用程序脚本



这两个函数在HTML代码中一起工作,我想把数据一起附加到电子表格的最后一行我试图获取最后一行和最后一列,但假设延迟了一点点,它将在另一条记录中替换,那么我如何传递所有输出,以便将其附加到行中呢。

我正在寻找的结果

Html代码

<html>
<head>
<base target="_top">
</head>
<body>

<button id = "find-me">Show my location</button><br/>
<p id = "status"></p>
<a id = "map-link" target="_blank"></a>
<p id = "1"></p>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous">
</script>
<script>
$(document).ready(function() {
google.script.run.withSuccessHandler(function (datareturned) {
alert(datareturned)
$('#1').text(datareturned)
}).getuser_email();
});
function geoFindMe() {

const status = document.querySelector('#status');
const mapLink = document.querySelector('#map-link');

mapLink.href = '';
mapLink.textContent = '';

function success(position) {

const latitude  = position.coords.latitude;
const longitude = position.coords.longitude;

google.script.run.saveCoordinates(latitude,longitude);

status.textContent = '';
mapLink.href = 'https://www.openstreetmap.org/#map=18/' + `${latitude}/${longitude}`;
mapLink.textContent = `Latitude: ${latitude} °, Longitude: ${longitude} °`;
}

function error() {
status.textContent = 'Unable to retrieve your location';
}

if(!navigator.geolocation) {
status.textContent = 'Geolocation is not supported by your browser';
} else {
status.textContent = 'Locating…';
navigator.geolocation.getCurrentPosition(success, error);
}

}

document.querySelector('#find-me').addEventListener('load', geoFindMe);
window.onload = geoFindMe
</script>
</body>
</html>

function saveCoordinates(latitude,longitude){
const sheet = SpreadsheetApp.getActiveSheet();
const rowContents = [latitude+","+longitude];
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
var lastCell = sheet.getRange(lastRow, lastColumn);
sheet.appendRow(rowContents);

}
function getuser_email(){
const email = Session.getActiveUser().getEmail();
return email;

您不显示HTML,所以我做了一个简单的例子:

<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<script>
function js_SaveCoordinates(latitude,longitude) {
google.script.run.withSuccessHandler( 
function(email) {
google.script.run.withSuccessHandler(
function (results) {
alert(JSON.stringify(results));
}
). saveCoordinates(email,latitude,longitude);
}
).getuser_email();
}
(function () { js_SaveCoordinates(1,2); }());
</script>
</body>
</html>

在Code.gs:中

function getuser_email() {
var email = Session.getActiveUser().getEmail();
return email;
}
function saveCoordinates(email,latitude,longitude) {
var sheet = SpreadsheetApp.getActiveSheet();
var rowContents = [[email,latitude,longitude]];  // 2 dimensional array with 1 row
sheet.getRange(sheet.getLastRow()+1,1,rowContents.length,rowContents[0].length).setValues(rowContents);
return { email: email, latitude: latitude, longitude: longitude };
}

我认为您的问题是如何使两个函数同步响应。你提到的延迟,我认为是从一个或另一个函数获取数据的延迟。

要将纬度和经度放入一个新行,您可以简单地执行此操作。

function saveCoordinates(latitude,longitude){
const sheet = SpreadsheetApp.getActiveSheet();
const rowContents = [latitude,longitude];
sheet.appendRow(rowContents);
}

相关内容

最新更新