I want to move data to another spreadsheet with a time limit, for example at 12PM the data will be moved automatically and then the data from the source will be deleted (I think by using triggers, but I don't know if it's true or not). For your information, previously I used checkbox but it was less efficient for me (script as below) because I have a lot of data that needs to be moved at the same time. Can anyone help me to make the script?. Thanks
function onMyEdit(e) {
//e.source.toast('Entry');
var sh=e.range.getSheet();
//Logger.log(JSON.stringify(e));
if(sh.getName()=='DatabaseTemp' && e.range.columnStart==13 && e.value=="TRUE") {
//e.source.toast('Access');
e.range.setValue('FALSE');
var tss=SpreadsheetApp.openById('1qx-I_mv8Zxxxxxxxxxxxxxxxxxxxxxx');
var tsh=tss.getSheetByName('Database');
var v=sh.getRange(e.range.rowStart,1,1,sh.getLastColumn()-3).getValues();
Logger.log(v);
tsh.getRange(tsh.getLastRow()+1,1,v.length,v[0].length).setValues(v);
sh.deleteRow(e.range.rowStart);
}
}
An example of a sheet name is "Data Source" and the destination or target spreadsheet "1OWqOlqy_pfZXXXXXXxxxxxxx" with sheet name is "Database"
function moveDataAutomatically() {
var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Data Source');
var targetSpreadsheet = SpreadsheetApp.openById('1wWN8iocE0ZxII50P7p-vs8eReum3OAku6hdH3zmtW1I');
var targetSheet = targetSpreadsheet.getSheetByName('Database');
var data = sourceSheet.getRange(2, 1, sourceSheet.getLastRow()-1, sourceSheet.getLastColumn()).getValues();
if (data.length > 0) {
targetSheet.getRange(targetSheet.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
for (var i = sourceSheet.getLastRow(); i > 1; i--) {
sourceSheet.deleteRow(i);
}
}
}
To run this script every day at 12 PM
, you need to set up a trigger in Google Sheets:
1.) In the Apps Script editor, click on the clock icon on the left toolbar to open the triggers page.
2.) Click + Add Trigger at the bottom-right.
3.) Set the function to moveDataAutomatically
.
4.) Set the deployment to Head.
5.) Choose "Time-driven" for the event source.
6.) Choose "Day timer" for the type of time-based trigger.
7.) Select the desired day and time for the trigger to run.
8.) Save
the trigger, and your script will now run automatically once per day.
function createTimeDrivenTriggers() {
ScriptApp.newTrigger('moveDataAutomatically')
.timeBased()
.everyDays(1)
.atHour(12)
.create();
}
Data Source
Database