I need your help to improve my apps script code in order to copy a dataset from 1 file to another file in Google Sheets. Currently it takes nearly 6 mins to finish this execution but our data is bigger by days. My details case is
- In the source file, sheet Database with the range is A1:U11834. But the row will be increased days by days.
- In the destination file, I also have a sheet name Database, and I want to clear the old data then copy the above source data into this sheet.
Here is my code.
function getdata(){
let ss = SpreadsheetApp
// open source file and sheet Database
let source_file = ss.openById("id_source_file")
let source_sht_copy = source_file.getSheetByName("Database")
// Get full range of data
let lr = source_sht_copy.getRange("A1").getDataRegion().getLastRow()
let actual_range = `A1:U${lr}`
Logger.log(actual_range)
let source_data = source_sht_copy.getRange(actual_range).getValues()
Logger.log("Copy Done")
// Open destination file
let dest_file = ss.openById("id_dest_file")
let dest_sht = dest_file.getSheetByName("Database")
// //clear content sheet database of destination file
dest_sht.clearContents()
Logger.log("Delete Old Data Done")
// // paste data from source file to destination file using method 'setValues'
dest_sht.getRange(actual_range).setValues(source_data)
Logger.log("Paste Done")
}
And this is the image show the time of processing.