Copy & Paste Formatting (1st time Posting)

108 views
Skip to first unread message

Jamie Schild

unread,
Nov 6, 2023, 11:10:21 AM11/6/23
to Google Apps Script Community
Good morning folks, 
I have a script that copy and pastes a central budget file to 7 department sheets. Whenever the data is pasted across, the format for the dates comes with a time (1/31/2024 1:00AM) and this causes some issues with formulas I have.

Could you help me with write code that copy and paste's with the proper format, pastes as values and doesn't overwrite the formate of end sheets format, OR formates the dates row after I paste it across?

What else am I missing that would help here?  Thank you so much for your help!  Fairly new to AppScripts and learning a lot from this group. 

Here's my code:
function copyData (sourceSpreadsheetId, sourceSheetName, destinationSpreadsheetId, destinationSheetName) {
// Get the source spreadsheet
const sourceSpreadsheet = SpreadsheetApp.openById(sourceSpreadsheetId);
const sourceSheet = sourceSpreadsheet.getSheetByName(sourceSheetName);

// Get the data range from the source sheet
const sourceRange = sourceSheet.getDataRange();
const sourceValues = sourceRange.getValues();

// Get the destination spreadsheet
const destinationSpreadsheet = SpreadsheetApp.openById(destinationSpreadsheetId);
const destinationSheet = destinationSpreadsheet.getSheetByName(destinationSheetName);

// Clear the contents of the destination sheet to ensure a fresh copy
destinationSheet.clearContents();

// Get the destination range in the destination sheet
const destinationRange = destinationSheet.getRange(1, 1, sourceValues.length, sourceValues[0].length);

// Copy the data to the destination sheet
destinationRange.setValues(sourceValues);
}

function updateBudget() {
// Delay for 1 minute (60,000 milliseconds)
Utilities.sleep(300000);
copyData ('url to source sheet', sourceSheetName, 'destination sheet url', destinationSheetName)} -> Leaving off for privacy; have this line for each of our 7 departments.


Laurie Nason

unread,
Nov 6, 2023, 11:42:09 PM11/6/23
to google-apps-sc...@googlegroups.com
Hi - you can try using getDisplayValues() instead of getValues() here....
const sourceValues = sourceRange.getDisplayValues();

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/a7a6f2f8-0c2e-4948-a702-66d36db27d93n%40googlegroups.com.


--

Laurie


Jamie Schild

unread,
Nov 7, 2023, 11:31:35 AM11/7/23
to google-apps-sc...@googlegroups.com
That worked!  Thanks Laurie.


You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/y3kRMHfw6Vc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/CA%2BA7ZWJEMj0yb9e19%3Dq1zngnS9whjssThTVnZtqg9A9Pic-%3D0A%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages