我喜欢使用脚本列出从开始到结束的日期并将其放在sheet2中。添加新的end和startdate后,脚本会自动添加日期。日期格式dd-mm-yy
我希望你能提供帮助,我搜索了许多我无法使用的剧本。感谢。
A B
startDate endDate
5-4-13 12-4-13
19-4-13 26-4-13
A
Dates
5-4-13
6-4-13
7-4-13
8-4-13
9-4-13
10-4-13
11-4-13
12-4-13
19-4-13
20-4-13
21-4-13
22-4-13
23-4-13
24-4-13
25-4-13
26-4-13 In column A
感谢大家的回复。 我已经google了很多,我已经粘贴了一些东西,几乎可以工作。 脚本不会打印最后一天,有时会从开始日期的前一天开始。 它可能与日期困难和setValue有关吗?
function getDates() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var export = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
var startRow = 2; // First row of data to process
var numRows = sheet.getLastRow()-1; // Number of rows to process
// Fetch the range of cells A:B
var dataRange = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn());
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i=0; i < data.length; i++) {
var row = data[i];
var startDate = row[0] //get start date
var endDate = row[1]; //get the end Date
endDate.setHours(0);
var e = Utilities.formatDate(endDate,'GMT+0100','d-M-yyyy');
//Logger.log(endDate+" "+e);
//Logger.log(date+" ");
startDate.setHours(0);
for (var k=0; k<9; k++) {
var date=new Date(startDate.getYear(),startDate.getMonth(),startDate.getDate() + k);
//Logger.log(d+" "+date);
var d = Utilities.formatDate(date,"GMT+0100","d-M-yyyy");
var last_row = export.getLastRow();
export.insertRowAfter(last_row);
var target_range = export.getRange("A"+(last_row + 1));
target_range.setValue(d);
if (e == d) break;
}
}
}
我找到了解决方案,但我认为这不是最好的解决方案。 我希望有更好的解决方案。所以我有11个小时加入GMT时间。 输出正常
我改变了; var e = Utilities.formatDate(endDate,'GMT + 0100','d-M-yyyy'); 至; var e = Utilities.formatDate(endDate,'GMT + 1200','d-M-yyyy');
和; var d = Utilities.formatDate(date,“GMT + 0100”,“d-M-yyyy”); 至; var d = Utilities.formatDate(date,“GMT + 1200”,“d-M-yyyy”);
答案 0 :(得分:1)
试试这个。您可能希望在出路时使用日期格式。
function genDates() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
// get the dates from sheet 1
var dates = ss.getSheetByName('Sheet1')
.getDataRange()
.getValues()
.reduce(function(p,c) {
var startDate = new Date (c[0]);
var endDate = new Date (c[1]);
while (startDate.getTime() <= endDate.getTime() ) {
p.push ( [new Date(startDate)] );
startDate.setDate( startDate.getDate() +1);
}
return p;
}, []);
// write the expanded dates to sheet 2
ss.getSheetByName ('Sheet2')
.clear()
.getRange(1,1,dates.length,dates[0].length)
.setValues (dates);
}
答案 1 :(得分:0)
查看谷歌提供的示例。像google drive-new file-script-spreadsheet sample..then google&#34; date addition google spreadsheet&#34; ...在电话上的床上...坏拇指... ...
答案 2 :(得分:0)
https://developers.google.com/apps-script/articles
更好?我用Google搜索了它....
答案 3 :(得分:0)
我起草了一个适合我的解决方案,虽然Google表格格式化日期的方式可能有点棘手。但是,您可以为整个工作表设置时区,这可以解决部分问题。请注意,Google建议使用appendRow()而不是setValue(),并且如果需要,此代码不会插入新行。当然,您可以轻松地添加它。我使用onEdit()触发器而不是菜单函数来完成此操作。根据您的要求,看似合理。
// assumes columns are already set to display dates in dd-mm-yy format
ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setSpreadsheetTimeZone("Etc/GMT-1"); // set spreadsheet timezone to +1:00 (see http://joda-time.sourceforge.net/timezones.html for other values)
sheet1 = ss.getSheetByName("Sheet1");
sheet2 = ss.getSheetByName("Sheet2");
Date.prototype.isEqualTo = function(date) {
return date.getDate() === this.getDate() && date.getMonth() === this.getMonth() && date.getYear() === this.getYear();
};
function updateValues(start, end) {
var dateList = makeDateList(start, end);
dateList.forEach( function(date) {
sheet2.appendRow(date);
});
}
function makeDateList(start, end) {
var newDate = new Date(start.getYear(), start.getMonth(), start.getDate());
var dateList = [];
do {
var date = newDate.getDate();
var month = newDate.getMonth();
var year = newDate.getYear();
var dateString = [date, month, year].join("-");
dateList.push([dateString]);
newDate = new Date(year, month, (date + 1));
} while (newDate.isEqualTo(end) === false);
return dateList;
}
function onEdit(e) {
if (ss.getActiveSheet().getName() === "Sheet1") {
var range = e.range; // you could also rewrite this function to accept ranges of dates instead of just one cell at a time
var row = range.getRow();
var column = range.getColumn();
var adjacentRange = sheet1.getRange(row, 1);
if (column === 2 && e.range.isBlank() === false && adjacentRange.isBlank() === false) {
var start = adjacentRange.getValue();
var end = e.range.getValue();
updateValues(start, end);
}
}
}