My information is set in columns (A to E), info paste in 2 rows (A2:A3, B2:B3…). What I want to happen is when info set in last column E (E2:E3, E5:E6 and more), new info paste with a new row in column A (A5:A6, B5:B6).
My info is in the spreadsheet:
This code paste info doesn’t work correctly:
JavaScript
x
26
26
1
function submitData1() {
2
3
4
var ss = SpreadsheetApp.getActiveSpreadsheet();
5
var formSS = ss.getSheetByName("Form"); //Form Sheet
6
var datasheet = ss.getSheetByName("Job"); //Data Sheet
7
8
//Input Values
9
var values1 = [[formSS.getRange("B6").getValue()], [formSS.getRange("B7").getValue()]];
10
var sr = 1
11
var sc = 1
12
var nr = 2
13
var nc = 1
14
15
for (var i=1;i<1;i++)
16
var workingCell = datasheet.getRange(i,7);
17
{
18
if (workingCell ==null)
19
{datasheet.getRange(sr,datasheet.getLastColumn()+1,nr,nc).setValues(values1)
20
}
21
else {datasheet.getRange(datasheet.getLastRow()+1, sc, nr, nc).setValues(values1)
22
sc= sc+1}
23
24
}
25
}
26
Advertisement
Answer
Here is a sample replication code:
JavaScript
1
23
23
1
function submitData1() {
2
3
var ss = SpreadsheetApp.getActiveSpreadsheet();
4
var datasheet = ss.getSheetByName("Job"); //Data Sheet
5
6
//Input Values
7
var values1 = [["Name X"], ["Job X"]];
8
var lastRow = datasheet.getLastRow();
9
var lastRow_ColCount = datasheet.getRange(lastRow,1,1,5).getValues().flat().filter(String).length;
10
Logger.log(lastRow);
11
Logger.log(lastRow_ColCount);
12
13
if(lastRow_ColCount==5){
14
//Add data on a new row, column A
15
datasheet.getRange(lastRow+2, 1,2,1).setValues(values1);
16
}
17
else{
18
//Add data on the next column in the same row
19
datasheet.getRange(lastRow-1, lastRow_ColCount+1,2,1).setValues(values1);
20
}
21
22
}
23
- I used a fixed value for the input data during this replication.
- Just replace the values from your original code that will be read from your form sheet
- You can also remove the logs in the script, I just used them to debug the code
What it does?
- Get the last row of the sheet, It will determine the last row that has a valid data
- Get the data/values of the last row from column A to column E. Use Array.flat() to change 2-d array to 1-d array. Use Array.filter() to get non empty cells then count the data available in the last row.
- If the current data available is 5, then the new data will be added on the new row, else the new data will be added on the next column