'How to prevent erase of cells in another Google Sheet file with importrange and make this process automatic
I am trying to set up 2 google sheet files where File 1 takes in quotes; code, description, quantity, unit cost, discount, price with discount, and total for in a table. And File 2 will copy over what is in File 1 and basically acts as long term records. I am using IMPORTRANGE to import the data.
To have a copy of File 1 each time there is a new record without eraising the other records, that's why I made this formula:
function copyFromOneSheetToAnother() {
var sheet1 = SpreadsheetApp.openById("10gfdMLuEZL_e6KDu_PRhDgArKFIivVj3SkwWI-kMDws");
var sheet2 = SpreadsheetApp.openById("10gfdMLuEZL_e6KDu_PRhDgArKFIivVj3SkwWI-kMDws");
var currDate = new Date();
var editedDate = new Date(currDate - 86400000);
var insert = sheet1.getSheetByName("Nicaragua");
var archive = sheet2.getSheetByName("NI");
var insertData = insert.getRange(2, 1, (sheet1.getRange('A:G').getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow()), 28);
for (row = 2; row <= insertData.getNumRows(); row++){
var data = insert.getRange(row, 1, 1, 28).getValues();
if (data[0][0] < currDate && data[0][0] > editedDate){
archive.getRange((archive.getLastRow() + 1), 1, 1, 28).setValues(data).setNumberFormat("MM/dd/yyyy");
}
}
}
But I when I run it is not working and I need this process happens automathic each time a new record is found
I've tried googling for my answer but I could not find one.
Solution 1:[1]
Don't know what your function has to do with you question but this will have a much greater chance of working.
If you wish it to run automatically then create a timebased trigger for it.
function copyFromOneSheetToAnother() {
const ss1 = SpreadsheetApp.openById("10gfdMLuEZL_e6KDu_PRhDgArKFIivVj3SkwWI-kMDws");
const ss2 = SpreadsheetApp.openById("10gfdMLuEZL_e6KDu_PRhDgArKFIivVj3SkwWI-kMDws");
const dt = new Date()
const today = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();
const yesterday =new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()-1).valueOf();
const sh1 = ss1.getSheetByName("Nicaragua");
const sh2 = ss2.getSheetByName("NI");
const vs1 = sh1.getRange(2, 1, sh1.getLastRow() - 1, 28).getValues();
vs1.forEach((r, i) => {
let dtv = new Date(r[0]);
if (dtv < today && dtv > yesterday) {
sh2.getRange(sh2.getLastRow() + 1, 1, 1, r.length).setValues(r).setNumberFormat("MM/dd/yyyy");
}
});
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Cooper |
