我目前正在为我的工作时间使用自定义时间跟踪器。
要清理由另一个脚本插入的数据,我需要删除到达和返回之间的所有行,这是通过App Script完成的。
错误似乎出在先前值与当前行值的比较中,如下面的代码所示。
我已经在网上搜索了其他情况,在大多数情况下比较变量而不是值时都犯了一个错误。当我使用“ getValue()”时,我不确定我的错误在哪里。
我已附加了运行时生成的日志文件。
我感谢任何注释和提示,尤其是因为我刚开始使用App Script时。
[18-08-08 05:50:06:542 PDT] B2:B rows: 10
[18-08-08 05:50:06:625 PDT] curr_date: Tue Jul 03 2018 00:00:00 GMT+0200 (CEST)
[18-08-08 05:50:06:731 PDT] previous_date: Tue Jul 03 2018 00:00:00 GMT+0200 (CEST)
[18-08-08 05:50:06:733 PDT] new day found, updating to: Tue Jul 03 2018 00:00:00 GMT+0200 (CEST)
[18-08-08 05:50:06:734 PDT] Deleting 0 middle elements
[18-08-08 05:50:06:736 PDT] curr_date: Wed Jul 04 2018 00:00:00 GMT+0200 (CEST)
[18-08-08 05:50:06:737 PDT] previous_date: Tue Jul 03 2018 00:00:00 GMT+0200 (CEST)
[18-08-08 05:50:06:738 PDT] new day found, updating to: Wed Jul 04 2018 00:00:00 GMT+0200 (CEST)
[18-08-08 05:50:06:739 PDT] Deleting 0 middle elements
[18-08-08 05:50:06:741 PDT] curr_date: Wed Jul 04 2018 00:00:00 GMT+0200 (CEST)
[18-08-08 05:50:06:742 PDT] previous_date: Wed Jul 04 2018 00:00:00 GMT+0200 (CEST)
[18-08-08 05:50:06:744 PDT] new day found, updating to: Wed Jul 04 2018 00:00:00 GMT+0200 (CEST)
[18-08-08 05:50:06:744 PDT] Deleting 0 middle elements
[18-08-08 05:50:06:746 PDT] curr_date: Wed Jul 04 2018 00:00:00 GMT+0200 (CEST)
[18-08-08 05:50:06:747 PDT] previous_date: Wed Jul 04 2018 00:00:00 GMT+0200 (CEST)
[18-08-08 05:50:06:748 PDT] new day found, updating to: Wed Jul 04 2018 00:00:00 GMT+0200 (CEST)
[18-08-08 05:50:06:749 PDT] Deleting 0 middle elements
[18-08-08 05:50:06:751 PDT] curr_date: Wed Aug 08 2018 00:00:00 GMT+0200 (CEST)
[18-08-08 05:50:06:752 PDT] previous_date: Wed Jul 04 2018 00:00:00 GMT+0200 (CEST)
[18-08-08 05:50:06:753 PDT] new day found, updating to: Wed Aug 08 2018 00:00:00 GMT+0200 (CEST)
[18-08-08 05:50:06:754 PDT] Deleting 0 middle elements
[18-08-08 05:50:06:755 PDT] skipping empty row: 8
[18-08-08 05:50:06:757 PDT] skipping empty row: 9
[18-08-08 05:50:06:758 PDT] skipping empty row: 10
[18-08-08 05:50:06:760 PDT] skipping empty row: 11
[18-08-08 05:50:06:761 PDT] All done, stopping
function del_duplicate()
{
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("raw");
var range = sheet.getRange(col_dates); // TODO: correct to use only filled cells
var n_rows = range.getNumRows();
Logger.log(col_dates + " rows: " + n_rows);
var previous_date = range.getCell(1, 1);
var middle_dates = [];
var curr_date;
for (var i = 2; i <= n_rows; i++) // start at second
{
curr_date = range.getCell(i, 1);
if(curr_date.getValue() === "")
{
Logger.log("skipping empty row: " + curr_date.getRow());
continue;
}
Logger.log("curr_date: " + curr_date.getValue());
Logger.log("previous_date: " + previous_date.getValue());
if (previous_date.getValue() === curr_date.getValue()) // same day as last value WTF!! Never equal
{
Logger.log("equal day found, pushing: " + curr_date.getValue());
middle_dates.push(curr_date); //add to end
}
else // new day in series
{
Logger.log("new day found, updating to: " + curr_date.getValue());
previous_date = curr_date;
// --- delete old ones
middle_dates.pop(); // don't delete last value
Logger.log("Deleting " + middle_dates.length + " middle elements")
for (var i_array = 0; i_array < middle_dates.length; i++)
{
var del_row = middle_dates[i_array].getRow();
Logger.log("Deleting row " + del_row )
//sheet.deleteRow(del_row);
}
// empty list for next date
middle_dates = [];
}
}
Logger.log("All done, stopping");
}
答案 0 :(得分:0)
@tehhowch的有用评论使我进一步调查。
显然,格式化为日期的单元格的值是JavaScript中的Date
对象。
我已在下面附上更新的代码,以供其他有相同问题的人参考。
再次感谢您的通知!
col_dates = "B2:B"
col_times = "C2:C"
function register()
{
SpreadsheetApp.getUi().createAddonMenu()
.addItem('Delete duplicates', 'del_duplicate')
.addToUi();
Logger.log("Registered UI");
}
function del_duplicate()
{
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("raw");
var range = sheet.getRange(col_dates); // TODO: correct to use only filled cells
var n_rows = range.getNumRows();
Logger.log(col_dates + " rows: " + n_rows);
var previous_date = range.getCell(1, 1);
var middle_dates = [];
var curr_date;
for (var i = 2; i <= n_rows; i++) // start at second
{
curr_date = range.getCell(i, 1);
if(curr_date.getValue() === "")
{
Logger.log("skipping empty row: " + curr_date.getRow());
continue;
}
// ------
var prev_date_value = previous_date.getValue();
var curr_date_value = curr_date.getValue();
//Logger.log("curr_date: " + curr_date_value);
//Logger.log("previous_date: " + prev_date_value);
if (prev_date_value.getDate() == curr_date_value.getDate()) // JS Date objects instead of cell value
{
Logger.log("equal day found, pushing: " + curr_date_value);
middle_dates.push(curr_date); //add to end
}
else // new day in series
{
//Logger.log("new day found, updating to: " + curr_date.getValue());
previous_date = curr_date;
// --- delete old ones
middle_dates.pop(); // don't delete last value
Logger.log("Deleting " + middle_dates.length + " middle elements")
for (var i_array = 0; i_array < middle_dates.length; i_array++)
{
var del_row = middle_dates[i_array].getRow();
Logger.log("Deleting row " + del_row )
sheet.deleteRow(del_row);
}
// empty list for next date
middle_dates = [];
}
}
Logger.log("All done, stopping");
}