0

我喜欢有一个脚本,列出从开始到结束日期的日期并将其放在 sheet2 中。添加新的结束日期和开始日期时,脚本会自动添加日期。日期格式 dd-mm-yy

我希望你能提供帮助,我已经搜索了很多我无法使用的脚本。谢谢。

表 1

A         B

startDate endDate

5-4-13    12-4-13

19-4-13   26-4-13

结果在 Sheet2

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

感谢大家的回复。我已经用谷歌搜索了很多,并且我已经将一些东西粘贴在一起几乎可以工作。该脚本不打印最后一天,有时它从开始日期的前一天开始。这可能与日期困难和 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;
}
}
}

我找到了解决方案,但我认为这不是最好的解决方案。我希望有更好的解决方案。所以我在 GMT 时间上增加了 11 个小时。并且输出没问题

我改变了; var e = Utilities.formatDate(endDate,'GMT+0100','dM-yyyy'); 至; var e = Utilities.formatDate(endDate,'GMT+1200','dM-yyyy');

和; var d = Utilities.formatDate(date,"GMT+0100","dM-yyyy"); 至; var d = Utilities.formatDate(date,"GMT+1200","dM-yyyy");

4

4 回答 4

1

Try this. You may want to play with the date formats on the way out.

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);

}
于 2016-01-18T13:02:20.000 回答
0

查找 google 提供的示例。像谷歌驱动器新文件脚本电子表格示例......然后谷歌“日期添加谷歌电子表格”......在手机上......糟糕的拇指打字机......

于 2013-03-28T01:58:25.957 回答
0

https://developers.google.com/apps-script/articles

更好的?我用谷歌搜索给你了......

于 2013-03-28T02:55:29.293 回答
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);
    }
  }
}
于 2015-02-14T19:28:25.093 回答