将变量传递到 .setValues() 中会清空单元格



当从表单输入数据并将其传递给Google Apps Script函数时使用.setValues((更新给定行的信息时,每个单元格返回空,并且这些单元格中的所有数据都将被删除

我正在努力为客户创建一个数据库。我正在从输入模式中获取变量并将它们传递给 Google Apps 脚本函数。当我直接提供字符串时,我可以让它正常运行,但是在填写表单并提交时,整个单元格范围返回空。如果单元格中有数据,则会将其删除。

当我切换到该范围的 5 行和 1 列时,它将使用输入的正确数据进行更新。所以我觉得这可能与我传递给应用程序脚本函数的数组有关。

任何人都知道为什么数据在作为 1 行和 5 列传递时不会填充单元格,而作为 1 列和 5 行传递时会返回正确的数据?

//Client-side function that passes variables to Apps Script function
function updateContact() {
let name = document.getElementById("name").value;
let address = document.getElementById("address").value;
let phone = document.getElementById("phone").value;
let email = document.getElementById("email").value;
let notes = document.getElementById("notes").value;
google.script.run.updateContact(name, address, phone, email, notes);
}
//Google Apps Script function to Replace Values in Range
function updateContact(name, address, phone, email, notes) {
var ss = SpreadsheetApp.openByUrl(
"https://docs.google.com/spreadsheets/d/1ek3XVSyoxmS-ef5dgph8KKpe4fmnyHSWxLl2-uH5xyg/edit#gid=1696663263"
);
var sheet = ss.getSheets()[0];
var range = sheet.getRange(2, 1, 1, 5);
range.setValues([[name, address, phone, email, notes]]);
}

这对我有用:

function updateContact(one,two,three) {
var one=one||1;
var two=two||2;
var three=three||3;
var ss = SpreadsheetApp.openById('ssid');
var sheet = ss.getSheetByName('Sheet1');
var range = sheet.getRange(2, 1, 1, 3);
range.setValues([[one, two, three]]);
}

最新更新