javascript - 无法让 setValue 在 GoogleScripts 中正常工作
问题描述
我对编码非常陌生,我正在尝试在 Google 表格中创建一个简单的按钮,该按钮将从“输入”选项卡中获取数据并填充“馈送”选项卡中相应列的最后一个空单元格。我可以记录喂食和日期的值,但似乎无法让它们移动。
这是代码:
//Gets data from 'Input' sheet
function recordData() {
let src = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Input'); //Source
let dst = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Feedings'); //Destination
let input = {};
input.milk = src.getRange('B2').getValue()
input.formula = src.getRange('B3').getValue()
let today = new Date();
today.date = today.getMonth()+1+'/'+today.getDate();
today.formatTime = function formatAMPM (time) {
var hours = time.getHours();
var minutes = time.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var endTime = hours + ":" + minutes + " "+ ampm;
return endTime;
}
today.time = today.formatTime (new Date);
Logger.log("formula = " + input.formula);
Logger.log("milk = " + input.milk);
Logger.log("date = "+ today.date);
Logger.log("time = "+ today.time);
//moves data from 'Input' to 'Feeding' sheet. **NOT WORKING**
function moveData () {
var dstFeedings = {};
dstFeedings.date = dst.getRange("Feedings!A2:A").getLastrow.setValues(today.date);
dstFeedings.time = dst.getRange("Feedings!B2:B").getLastRow.setValues(today.time);
dstFeedings.formula = dst.getRange("Feedings!C2:C").getLastRow.setValues(input.formula);
dstFeedings.milk = dst.getRange("Feedings!D2:D").getLastRow.setValues(input.milk);
return dstFeedings;
}
}
解决方案
问题中的代码至少有两个问题,getLastRow
并且setValues
,在
dstFeedings.date = dst.getRange("Feedings!A2:A").getLastrow.setValues(today.date);
dstFeedings.time = dst.getRange("Feedings!B2:B").getLastRow.setValues(today.time);
dstFeedings.formula = dst.getRange("Feedings!C2:C").getLastRow.setValues(input.formula);
dstFeedings.milk = dst.getRange("Feedings!D2:D").getLastRow.setValues(input.milk);
getLastRow
没有setValues
方法。setValues
要求和 Array 对每一行都有一个 Array,即内部 Array,对于每一行的单元格应该有一个值或 Date 对象。由于使用的变量包含单个值 / Date 对象,setValue
因此应使用而不是setValues
.
有关的
推荐阅读
- django - Django:无法在 DRF RetrieveUpdateDestroyAPIView 中获取当前登录用户
- javascript - 如何在 EJS 中呈现列表?
- python - 是否可以使用 numpy 避免此循环?
- python - Python从多个数据帧创建数据帧,其中一个具有多列
- laravel - 尝试测试注册路由的 laravel 身份验证时
- javascript - 使用 JavaScript 修改文件输入附件
- javascript - 单击按钮时如何在 ReactJS 中切换类名?
- android - 在Android和iOS上的OpenGL中围绕图像绘制恒定宽度的边框
- kubernetes - Prometheus 中的 Kubernetes pod QoS 类
- saml-2.0 - ASP.NET Core 和 SAML 身份验证