简体   繁体   中英

How to copy data and formatting from google sheet a to existing sheet b using google scripts without adding timestamp to dates

When using .getValues and .setValues to copy values from sheet A to sheet B, all values copy but date values in sheet A have no timestamp but a timestamp is added to date values in sheet b

Aside from below code, I have tried or researched:

sheet.copyTo(destination);// only to create new sheet
setValue(new Date(new Date().setHours(0,0,0,0))).setNumberFormat('MM/dd/yyyy');//removes timestamp but sets todays date
sonosFcstDestDateCells.setNumberFormat('dd/MM/yyyy');//did not remove date timestamp

Here is current code:

 function cloneGoogleSheet(ssA, ssB) {
 // source docs
 var sssFcst = SpreadsheetApp.openByUrl('url');
 // target/destination spreadsheet
 var tss = SpreadsheetApp.openByUrl('url');
 //set var for ssA and ssB source and destination
 var ssAFcstSource = sssFcst.getSheetByName("ssA"); //
 var ssAFcstSourceRange = ssAFcstSource.getDataRange(); //
 var ssAFcstSourceA1Range = ssAFcstSourceRange.getA1Notation(); //
 var ssAFcstSourceData = ssAFcstSourceRange.getValues(); //
 var ssBFcstDest = tss.getSheetByName("ssB"); //
 var ssBFcstDestDateCells = ssBFcstDest.getRange("5:5");
 var ssAFcstSourceDateCells = ssAFcstSource.getRange("5:5");
 var sNF = ssAFcstSource.getDataRange().getNumberFormats();
 // Clear the destination sheet before paste
 ssBFcstDest.clear({contentsOnly: true});
 // set the target range to the values of the source data
 ssBFcstDest.getRange(ssAFcstSourceA1Range).setValues(ssAFcstSourceData);
 //this duplicates all formats from ssA to ssB, but does not remove added date 
 //timestamp
 ssBFcstDest.getRange(1,1,ssAFcstSourceData.length,ssAFcstSourceData[0].length).setValues(ssAFcstSourceData).setNumberFormats(sNF);
 };

Desired result is to have values copy from sheet A to Sheet B without adding timestamp to date values

There are many ways to copy data from the sheet of one spreadsheet to the sheet of another spreadsheet sheet.

In this example, I created a date field and entered two records: 23 March 2019 and 31 December 1985

This is the output using the following methods:

copyTo() Ref

  • 23 March 2019
  • 31 December 1985

getValues()/setValues() Ref

  • 23/03/2019
  • 31/12/1985

getDisplayValues()/setValues() Ref

  • 23 March 2019
  • 31 December 1985

getValues()/setValues() - push to array

  • 23/03/2019
  • 31/12/1985

Utilities.formatDate(ssAData[i][0], Session.getScriptTimeZone() ,"dd MMMM yyyy") Ref

  • 23 March 2019
  • 31 December 1985

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