I have two spreadsheets, one is called "Input data" and the other is called "Assignment". If a value/content(in my case a name) in column C of "Assignment" does not occur in column C of "Input data", the specific row gets deleted by this formula that works completely fine:
function checkName() {
var s1 = SpreadsheetApp.getActive().getSheetByName("Input Data");
var col1 = s1.getRange("C3:C").getValues();
var s2 = SpreadsheetApp.getActive().getSheetByName("Assignment");
var col2 = s2.getRange("C4:C").getValues();
for (var i=col2.length-1; i>=0; i--) {
var found = false;
for (var j=0; j<col1.length; j++) {
if (col1[j][0] == col2[i][0]) {
found = true;
break;
}
}
if (!found) {
s2.deleteRow(i+4);
}
}
}
Is it possible to delete only the contents of these specific rows or to delete only the content in specific columns of these specific rows? Like only column A-F of row 30 for example (if it fits to the criteria that I have described above). I tried s2.deleteContents(i+4)
but that does not really work... Could you help me please?
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
In this pattern, your script was modified. Please modify your script as follows.
s2.deleteRow(i+4);
To:
s2.getRange(i+4, 1, 1, 6).clearContent();
In this pattern, at first, the ranges for clearing are retrieved. And the range list is cleared by clearContent()
. The process cost might be able to be reduced a little.
function checkName() {
var s1 = SpreadsheetApp.getActive().getSheetByName("Input Data");
var col1 = s1.getRange("C3:C" + s1.getLastRow()).getValues().map(function(e) {return e[0]});
var s2 = SpreadsheetApp.getActive().getSheetByName("Assignment");
var col2Range = s2.getRange("A4:F" + s2.getLastRow());
var rangeList = col2Range.getValues().reduce(function(ar, e, i) {
if (col1.indexOf(e[2]) == -1) ar.push("A" + (i + 4) + ":F" + (i + 4));
return ar;
}, []);
// if (rangeList.length > 0) rangeList.push("A30:F30");
s2.getRangeList(rangeList).clearContent();
}
Like only column A-F of row 30 for example
, if you want to also clear the content of "A30:F30", please remove //
of the line of // if (rangeList.length > 0) rangeList.push("A30:F30");
. By this, when above condition occurs, the content of "A30:F30" is also cleared. But about this, I'm not sure whether I could correctly understand about your goal. I apologize for this.If I misunderstood your question and this was not the result you want, I apologize.
Michael, Ballack
and Michael, BALLACK
as the same value.From your replying, I could understand about that. For above case, how about the following modification? In this case, using toUpperCase()
, both values from Input Data
and Assignment
are compared.
function checkName() {
var s1 = SpreadsheetApp.getActive().getSheetByName("Input Data");
var col1 = s1.getRange("C3:C").getValues();
var s2 = SpreadsheetApp.getActive().getSheetByName("Assignment");
var col2 = s2.getRange("C4:C").getValues();
var found;
for (var i=col2.length-1; i>=0; i--) {
found = false;
for (var j=0; j<col1.length; j++) {
if (col1[j][0].toUpperCase() == col2[i][0].toUpperCase()) { // Modified
found = true;
break;
}
}
if (!found) {
s2.getRange(i+4, 1, 1, 6).clearContent(); // Modified
}
}
}