简体   繁体   中英

Same date value but the spreadsheet formula says they are different values when using '.copyTo' and '.setValue'

With the formula in A1 being:

=TEXT(TODAY(),"DD/MM/YYYY")

Copy using:

function onEdit(e) {
  var ss = SpreadsheetApp.getActive();
  ss.getRange('Dados Atuais!A1').copyTo(ss.getRange('Dados Atuais!B1'), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
}

When I try to specify the value directly by script (I will summarize the script showing only the part of the value):

sheet.getRange(row + 1, 2).setValue(Utilities.formatDate(new Date(), "America/Sao_Paulo", "dd/MM/yyyy"));

When I try to compare the values:

=B1=B2

The result is FALSE

Example:
在此处输入图像描述

REQUEST:

How can I do so that I can make the second script model deliver a value equal to that of the first? The first is mostly used, so when I need to use the second script, the formulas in the spreadsheet need to analyze that both are the same.

I thought that in your script, your 1st script puts the value as the string type and your 2nd script puts the value as the date object. I thought that this might be the reason of your issue.

From your following question,

How can I do so that I can make the second script model deliver a value equal to that of the first? The first is mostly used, so when I need to use the second script, the formulas in the spreadsheet need to analyze that both are the same.

In this case, for your 2nd script, how about the following modification?

From:

sheet.getRange(row + 1, 2).setValue(Utilities.formatDate(new Date(), "America/Sao_Paulo", "dd/MM/yyyy"));

To:

sheet.getRange(row + 1, 2).setNumberFormat("@").setValue(Utilities.formatDate(new Date(), "America/Sao_Paulo", "dd/MM/yyyy"));

Reference:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM