在从Google表格复制到另一个地方时,将日期增加7天。

huangapple go评论60阅读模式
英文:

adding 7days to a date when copying from a google sheet to an other

问题

这段代码是用来从一个电子表格复制和整理数据到另一个电子表格的。您提到的问题是要将某些日期加上7天,但您不知道如何实现。这是代码中相关部分的翻译:

// 将以下代码部分翻译
[valueRange[5], valueRange[7], valueRange[9], valueRange[17] /**+ ' 7days'*/, valueRange[19], 'phase de fabrication'/**color green*/],//production time

这段代码中的 valueRange[17] 后面的注释部分 /**+ ' 7days'*/ 表示要将日期加上7天。如果您需要实现这个功能,您可以使用 Google Apps Script 中的 Date 对象来进行日期计算。

英文:

here is a code I have done to copy and organise data from a spreadsheet to an other one.

function GENERAL() {
  var Cop = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("GO");
  var LP = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("0_Test-LISTE PROJETS");
  var values = LP.getRange('A4:V' + LP.getLastRow()).getValues();
  var res = values.flatMap(valueRange =>
    [
      [valueRange[5], valueRange[7], valueRange[9], valueRange[16], valueRange[20], 'duréé projet' /**color red*/],//project lenght
      [valueRange[5], valueRange[7], valueRange[9], valueRange[16], valueRange[17], 'phase de conception'/**color BLUE*/],//research lenght
      [valueRange[5], valueRange[7], valueRange[9], valueRange[17], valueRange[17]/**+ ' 7days'*/ , 'attente retour BAT'/**color white*/],//waiting for client
      [valueRange[5], valueRange[7], valueRange[9], valueRange[17] /**+ ' 7days'*/, valueRange[19], 'phase de fabrication'/**color green*/],//production time
      [valueRange[5], valueRange[7], valueRange[9], valueRange[19], valueRange[20], 'phase installation'/**color purple*/]//installation on site
    ]
  );
  Cop.getRange(2, 1, res.length, res[0].length).setValues(res);

}

as mentionned on the tittle, the problem is that I want to add 7 days to some of the date :
"valueRange[17]/**+ ' 7days'*"
But I just don't know how...
Thanks for your help !

答案1

得分: 0

From:

var res = values.flatMap(valueRange =>
  [
    [valueRange[5], valueRange[7], valueRange[9], valueRange[16], valueRange[20], 'duréé projet' /**color red*/],//project length
    [valueRange[5], valueRange[7], valueRange[9], valueRange[16], valueRange[17], 'phase de conception'/**color BLUE*/],//research length
    [valueRange[5], valueRange[7], valueRange[9], valueRange[17], valueRange[17]/**+ ' 7days'*/, 'attente retour BAT'/**color white*/],//waiting for client
    [valueRange[5], valueRange[7], valueRange[9], valueRange[17] /**+ ' 7days'*/, valueRange[19], 'phase de fabrication'/**color green*/],//production time
    [valueRange[5], valueRange[7], valueRange[9], valueRange[19], valueRange[20], 'phase installation'/**color purple*/]//installation on site
  ]
);

To:

var res = values.flatMap(valueRange => {
  valueRange[17] = new Date(valueRange[17].getTime() + (7 * 86400000));
  return [
    [valueRange[5], valueRange[7], valueRange[9], valueRange[16], valueRange[20], 'duréé projet' /**color red*/],//project length
    [valueRange[5], valueRange[7], valueRange[9], valueRange[16], valueRange[17], 'phase de conception'/**color BLUE*/],//research length
    [valueRange[5], valueRange[7], valueRange[9], valueRange[17], valueRange[17]/**+ ' 7days'*/, 'attente retour BAT'/**color white*/],//waiting for client
    [valueRange[5], valueRange[7], valueRange[9], valueRange[17] /**+ ' 7days'*/, valueRange[19], 'phase de fabrication'/**color green*/],//production time
    [valueRange[5], valueRange[7], valueRange[9], valueRange[19], valueRange[20], 'phase installation'/**color purple*/]//installation on site
  ];
});

Note: When an error like valueRange[17].getTime is not a function occurs, it might indicate that the value of valueRange[17] is not a date object. In this case, the script will handle it as a date object and add 7 days.

英文:

I believe your goal is as follows.

  • You want to add 7 days to the value of valueRange[17] in your script.
  • I suppose that your value of valueRange[17] is the date object.

In this case, how about the following modification?

From:

var res = values.flatMap(valueRange =>
  [
    [valueRange[5], valueRange[7], valueRange[9], valueRange[16], valueRange[20], 'duréé projet' /**color red*/],//project lenght
    [valueRange[5], valueRange[7], valueRange[9], valueRange[16], valueRange[17], 'phase de conception'/**color BLUE*/],//research lenght
    [valueRange[5], valueRange[7], valueRange[9], valueRange[17], valueRange[17]/**+ ' 7days'*/ , 'attente retour BAT'/**color white*/],//waiting for client
    [valueRange[5], valueRange[7], valueRange[9], valueRange[17] /**+ ' 7days'*/, valueRange[19], 'phase de fabrication'/**color green*/],//production time
    [valueRange[5], valueRange[7], valueRange[9], valueRange[19], valueRange[20], 'phase installation'/**color purple*/]//installation on site
  ]
);

To:

var res = values.flatMap(valueRange => {
  valueRange[17] = new Date(valueRange[17].getTime() + (7 * 86400000));
  return [
    [valueRange[5], valueRange[7], valueRange[9], valueRange[16], valueRange[20], 'duréé projet' /**color red*/],//project lenght
    [valueRange[5], valueRange[7], valueRange[9], valueRange[16], valueRange[17], 'phase de conception'/**color BLUE*/],//research lenght
    [valueRange[5], valueRange[7], valueRange[9], valueRange[17], valueRange[17]/**+ ' 7days'*/, 'attente retour BAT'/**color white*/],//waiting for client
    [valueRange[5], valueRange[7], valueRange[9], valueRange[17] /**+ ' 7days'*/, valueRange[19], 'phase de fabrication'/**color green*/],//production time
    [valueRange[5], valueRange[7], valueRange[9], valueRange[19], valueRange[20], 'phase installation'/**color purple*/]//installation on site
  ];
});

Note:

  • When an error like valueRange[17].getTime is not a function occurs, I think that the value of valueRange[17] is not the date object. In that case, can you provide the sample value of valueRange[17]?

Added:

From I still wanted to be able to use [17], in this case, how about the following modification? The converted value from valueRange[17] is put to temp.

To:

var res = values.flatMap(valueRange => {

  // var temp = new Date(valueRange[17].getTime() + (7 * 86400000));
  var temp = valueRange[17] instanceof Date ? new Date(valueRange[17].getTime() + (7 * 86400000)) : valueRange[17];

  return [
    [valueRange[5], valueRange[7], valueRange[9], valueRange[16], valueRange[20], 'duréé projet' /**color red*/],//project lenght
    [valueRange[5], valueRange[7], valueRange[9], valueRange[16], temp, 'phase de conception'/**color BLUE*/],//research lenght
    [valueRange[5], valueRange[7], valueRange[9], valueRange[17], temp/**+ ' 7days'*/, 'attente retour BAT'/**color white*/],//waiting for client
    [valueRange[5], valueRange[7], valueRange[9], temp /**+ ' 7days'*/, valueRange[19], 'phase de fabrication'/**color green*/],//production time
    [valueRange[5], valueRange[7], valueRange[9], valueRange[19], valueRange[20], 'phase installation'/**color purple*/]//installation on site
  ];
});
  • About your new issue of valueRange[17].getTime is not a function, I have already mentioned it in my answer. For this, I updated the above script. Please confirm it.

答案2

得分: 0

Thanks! it helps, it was almost what I need, cause I still wanted to be able to use [17]

so I modify your code just a little bit:

function GENERAL() {
  var Cop = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("GO");
  var LP = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("0_Test-LISTE PROJETS");
  var values = LP.getRange('A4:V' + LP.getLastRow()).getValues();
  var res = values.flatMap(valueRange => {
    valueRange[177] = new Date(valueRange[17].getTime() + (7 * 86400000));
    return [
      [valueRange[5], valueRange[7], valueRange[9], valueRange[16], valueRange[20], 'durée projet' /**color red*/], //project length
      [valueRange[5], valueRange[7], valueRange[9], valueRange[16], valueRange[17], 'phase de conception' /**color BLUE*/], //research length
      [valueRange[5], valueRange[7], valueRange[9], valueRange[17], valueRange[177], 'attente retour BAT' /**color white*/], //waiting for client
      [valueRange[5], valueRange[7], valueRange[9], valueRange[177], valueRange[19], 'phase de fabrication' /**color green*/], //production time
      [valueRange[5], valueRange[7], valueRange[9], valueRange[19], valueRange[20], 'phase installation' /**color purple*/] //installation on site
    ];
  });
  Cop.getRange(2, 1, res.length, res[0].length).setValues(res);
}

Now, it's perfectly doing what I need! Thanks!

<details>
<summary>英文:</summary>

Thanks ! it helps, it was almost what I need, cause I still wanted to be able to use [17]

so I modify your code just a little bit :


function GENERAL() {
var Cop = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("GO");
var LP = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("0_Test-LISTE PROJETS");
var values = LP.getRange('A4:V' + LP.getLastRow()).getValues();
var res = values.flatMap(valueRange => {
valueRange[177] = new Date(valueRange[17].getTime() + (7 * 86400000));
return [
[valueRange[5], valueRange[7], valueRange[9], valueRange[16], valueRange[20], 'duréé projet' /*color red/],//project lenght
[valueRange[5], valueRange[7], valueRange[9], valueRange[16], valueRange[17], 'phase de conception'/*color BLUE/],//research lenght
[valueRange[5], valueRange[7], valueRange[9], valueRange[17], valueRange[177], 'attente retour BAT'/*color white/],//waiting for client
[valueRange[5], valueRange[7], valueRange[9], valueRange[177], valueRange[19], 'phase de fabrication'/*color green/],//production time
[valueRange[5], valueRange[7], valueRange[9], valueRange[19], valueRange[20], 'phase installation'/*color purple/]//installation on site
];
});
Cop.getRange(2, 1, res.length, res[0].length).setValues(res);


know, it&#39;s perfecly doing what I need ! Thanks !
</details>

huangapple
  • 本文由 发表于 2023年4月11日 15:29:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983415.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定