I'm doing this as part of the internship. I found here most part of what I needed but still can't complete it because I'm lacking in programmation.
I have 3 SpreadSheets with their IDs:
- SpreadSheet1 has some data stored in it,
- SpreadSheet2 is the one where the script is running on,
- SpreadSheet3 is a backup of SpreadSheet2.
In SpreadSheet2 when I imput something in column A or B the script should look for that value on column A of SpreadSheet1, if the value is the same than it has to copy the corresponding row right after the input (from A to Z if or from B to Z, depending on who got the match) and delete that row from SpreadSheet1. If the value isn't there than the imput should just be deleted.
When i type 0 on column B in the Spreadsheet2 the script should copy all the data present there should on SpreadSheet3 (first row available and 0 should not be copied) than it should erase all rows except the first one from SpreadSheet2.
Possibly this script should be available to use on different SpreadSheets simply by copying it, for example 3 computers should use different SpreadSheets with this script working on the same data from SpreadSheet1 and backuping all in SpreadSheet3.
Here is a visual example of what I'm trying to do
Sorry for bad English and thanks in advance
function example1(e) {
const range = e.range;
const sheet = range.getSheet();
if (sheet.getSheetName() != "Sheet1" || range.getColumn() != 1 || range.getRow() == 1) return;
const value = range.getValue();
const values = sheet.getRange("A2:A" + sheet.getLastRow()).getValues();
values.splice(range.getRow() - 2, 1);
if (values.flat().includes(value)) {
range.clearContent();
return;
}
const srcSheet = e.source.SpreadsheetApp.openById("SSheetID1");
const srcSheet3 = e.source.SpreadsheetApp.openById("SSheetID3");
const range3 = e.range;
const sheet = range3.srcSheet3;
const srcRange = srcSheet.getRange("A2:A" + srcSheet.getLastRow()).createTextFinder(value).matchEntireCell(true).findNext();
if (srcRange) {
srcRange.offset(0, 1, 1, srcSheet.getLastColumn() - 1).copyTo(range.offset(0, 1));
srcRange.offset(0, 1, 1, srcSheet.getLastColumn() - 1).copyTo(range3.offset(0,1));
srcRange.clearContent();
}
else {
range.clearContent();
return;
}
}
e.source.SpreadsheetApp
this is wrong, remove thee.source
part. Alsorange3.srcSheet3
is wrong. Is like you are callinge.range.e.source.SpreadsheetApp.openById("SSheetID3")
. – soMario