Question

Please help this non-expert -- his job depends upon it.

I have a Google Form that feeds answers into a sheet on a Google Spreadsheet -- let's call it the "main sheet." One of the form's questions asks for a job number. I have been able to figure out how to take each unique job number respondents have entered and create a sheet by that name in the same spreadsheet. I even have copied the headers for the form's answers at the top of each of the new sheets.

What I cannot get to work is rifling through all of the answers in the main sheet, sans the column headers, and copy the rows to the sheet names based on a conditional match with the job number value equaling the sheet name value. So if someone applies for job number 65, that response gets copied from the main sheet to sheet with the name "65."

For starters, I may have the loops set up incorrectly, trying to exclude the header in the main sheet and creating the array of all of the sheet names.

But a second problem I have is that I need to use the variables for both the sheet name value and the job number value. I need to be able to account for an ever increasing job numbers. Staff do not want to have to create a new sheet with every new job number -- they want that done automatically when a user fills in a new job number.

I am happy to share my work, as it is, with anyone who can help point me in the right direction.

+++

Solved, so I yanked down the link to the spreadsheet. In spite of myself, it appears, this community was able to help me out. Thanks.

Was it helpful?

Solution

This function that I wrote will help you find your job number:

function searchColumn(value, rangeValues) {

  for (var i = 1; i < rangeValues.length; ++ i) {
    if (rangeValues[i] == value) return i;
  }

  return -1;
}

Give it an array of values (usually by Range.getValues()), and the value you want to search and it will return the row number where that value was found. If it didn't find it, it returns -1.

example:

// Gets all data in the first column of the sheet
var valuestoSearch = mySheet.getRange(1, 1, sheet.getLastRow()).getValues();
// then
var rowNum = searchColumn(job_number, valuesToSearch);

Now that you have the row number, you can use:

var rowData = mySheet.getRange(rowNum, 1, 1, mySheet.getLastColumn()).getValues();

This will give you all the values in that row up to the last column in the sheet.

Once you have that data, you can copy it to another sheet by using:

 var jobSheet = mySpreadsheet.getSheetByName(job_number);
 jobSheet.getRange(jobSheet.getLastRow() + 1, 1, 1, rowData.length).setValues(rowData);

Or something very similar to this. Then you may want to erase the data in the old spreadsheet so that you don't keep repeating the same operation. But that's up to you how you want to handle that. Take a good look at the documentation for SpreadsheetApp, as it has everything you're looking for and more :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top