I have two tables, namely Table A and Table B. Each table I want to identify the name of the table in column C matches the table names that have been defined in column A.
The script I've made is like this:
tableName () function {
let ss = SpreadsheetApp.getActiveSpreadsheet ();
let cColumn1 = ss.getRange ('C3: C7');
let cColumn2 = ss.getRange ('C11: C15');
let tableName1 = ss.getRange ('C2'). getValue ();
let tableName2 = ss.getRange ('C9'). getValue ();
cColumn1.setValues (tableName1);
cColumn2.setValues (tableName2);
}
But my script is still manual, we have to specify the start line and end line of the table.
To identify the name of the table I've ever made with a formula, so with this formula, we don't need to specify the position the name of the reference table.
= ARRAYFORMULA (IF ((B: B = "") + (B: B = "Name") ,, VLOOKUP (ROW (A: A), IF (LEN (REGEXEXTRACT (A: A, "T\D+") ), {ROW (A: A), REGEXEXTRACT (A: A, "T\D+")}, 0), 2)))
This formula is very heavy if the amount of data is large.
Best Answer
You have Table A and Table B; they are stacked, one above the other in columns A, B and C and there is no other data in Columns A, B or C. Each table may be any number of rows long and there is a single blank row between each table. The name of Table A is in cell A1; the name of Table B is in Column A in the row following the blank row after Table A.
You want to populate cells in Column C with the respective table names. You have a formula (Cell E1) but this formula takes too long if there is a lot of data. So you want a script to populate cells in Column C and the script must dynamically calculate the ranges that apply to each Table.
Though the script in your question is manually updated, it fails because
setValues()
expects a two dimensional array (the values must match the dimensions of the range) but your input value is the value of a single cell.Points to note:
getLast Row()
is not used because it will return the last row for the entire sheet and this does not necessarily represent the data in Column A.filter
method), a value of 1 (one) must be added to "ALast" to determine the actual last row of content.setValues()
method.