gpt4 book ai didi

javascript - GSheet 的 Google 应用脚本 - 在 B 列上查找今天的日期,并在同一行的 C 列上写入当前时间

转载 作者:行者123 更新时间:2023-12-02 21:35:02 26 4
gpt4 key购买 nike

我正在尝试编写一个应用程序脚本并将其分配给一个按钮。当按下按钮时,它将激活一个我命名为 clockin() 的函数。这个函数的作用是在 B 列上查找今天的日期,并在 C 列上写入当前时间。问题是这段代码没有在定义的单元格上写入任何值,这有点糟糕。我是 JavaScript 新手,因此需要您的帮助。我的代码如下:

function todayDateNowTime () {
const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var today = new Date()
var month = monthNames[today.getMonth()]; //months from 1-12
var day = today.getDate();
var year = today.getFullYear();
var seconds = today.getSeconds();
var minutes = today.getMinutes();
var hour = today.getHours();

var todayDate = day+"-"+month+"-"+year;
var nowTime = hour+":"+minutes+":"+seconds;

console.log(todayDate);
console.log(nowTime);

return todayDate, nowTime;
}


function clockin(todayDate, nowTime) {
todayDate, nowTime = todayDateNowTime();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = sheet.getDataRange().getValues();
for(var i = 0; i<data.length;i++){
if(data[i][1] == todayDate) { //[1] because column B
var range = SpreadsheetApp.getActiveSpreadsheet().getActiveCell("C"+i)
range.setValue(nowTime);
}
}
}

我已将我的 gsheet 公开可供查看 here .

如果有帮助的话,我还在这里提供了一个屏幕截图: enter image description here

最佳答案

我对您的代码做了一些小调整。看看现在是否有效?

function todayDateNowTime () {
const monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct", "Nov", "Dec"];
var today = new Date()
var month = monthNames[today.getMonth()]; //months from 1-12
var day = today.getDate();
day = day < 10 ? "0" + day : day;
var year = today.getFullYear();
var seconds = today.getSeconds();
var minutes = today.getMinutes();
var hour = today.getHours();
var todayDate = day+"-"+month+"-"+year;
var nowTime = hour+":"+minutes+":"+seconds;

return [todayDate, nowTime];
}

function clockin() {
var dateAndTime = todayDateNowTime();
var todayDate = dateAndTime[0];
var nowTime = dateAndTime[1];
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = sheet.getDataRange().getDisplayValues();
for(var i = 0; i<data.length;i++){
if(data[i][1] == todayDate) { //[1] because column B
var range = sheet.getRange(i+1, 3)
range.setValue(nowTime);
}
}
}

关于javascript - GSheet 的 Google 应用脚本 - 在 B 列上查找今天的日期,并在同一行的 C 列上写入当前时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60520627/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com