使用公式进行转换的行复制

时间:2018-06-04 08:04:15

标签: google-sheets

示例sheet已关联。

将表格作为;

fromDate    toDate      Data
2018-01-01  2018-01-05  A
2018-02-01  2018-02-03  B
... so on

需要更改/转换为以下表单,其中日期范围中的每一天都已转换为行。

Date        Data
2018-01-01  A
2018-01-02  A
2018-01-03  A
2018-01-04  A
2018-01-05  A
2018-02-01  B
2018-02-02  B
2018-02-03  B
... so on

使用公式可以吗?

编辑1;添加Sheets自定义功能来完成相同的操作。再次,这可以使用内置公式吗?

function DateRangeToRows(values) {
  var new_values=[];
  for (var r=0; r<values.length; r++) {
    var stt=values[r][0], end=values[r][1];
    while (stt<=end) {
      new_values.push([new Date(stt)].concat(values[r].slice(2)));
      stt.setDate(stt.getDate()+1);
    }
  }
  return new_values;
}

2 个答案:

答案 0 :(得分:1)

从您的第一个fromDate到上一个toDate制作一个列表(系列填充),查找相应的Data值并删除后者不存在的行。

答案 1 :(得分:1)

The idea is to build a 2d array where each row is the different days in the date range, then split it out into separate rows, but at the moment I'm hard-coding the maximum length of the date range as 10 just to test it

=ArrayFormula(split(transpose(split(textjoin("#",true,if((transpose(row(1:10))+A2:A3-1)>B2:B3,"",transpose(row(1:10))+A2:A3-1&"|"&C2:C3)),"#")),"|"))

The next step will be to replace the 1:10 with the maximum date range from the data, using Indirect which leads to this:

=ArrayFormula(split(transpose(split(textjoin("#",true,if((transpose(row(indirect("1:"&max(B2:B3-A2:A3)+1)))+A2:A3-1)>B2:B3,"",transpose(row(indirect("1:"&max(B2:B3-A2:A3)+1)))+A2:A3-1&"|"&C2:C3)),"#")),"|"))

It isn't possible to change the references to full columns (A2:A etc) because it would need the sheet to have extra rows, but you can choose an arbitrary number of rows and select only used rows:

=ArrayFormula(query(split(transpose(split(textjoin("#",true,if((transpose(row(indirect("1:"&max(B2:B10-A2:A10)+1)))+A2:A10-1)>B2:B10,"",transpose(row(indirect("1:"&max(B2:B10-A2:A10)+1)))+A2:A10-1&"|"&C2:C10)),"#")),"|"),"select * where Col1>0"))

enter image description here