问题
I'm still fairly new with Google Scripts. I have a student roster Google Sheet with demographic information, including ID numbers. I also have a responses Sheet that automatically collects email addresses on Column B (email addresses are created by the ID number plus "@foo.org"). I'm trying to get my script to parse through auto-collected emails, map the ID portion of the email onto column C. Then I'm using for loops to check the extracted ID number against my main roster, and map the remaining information for the same student on the responses sheet.
As of now, I'm only able to correctly map the first row in the responses sheet; As of the second row I only see the numeric IDs but it doesn't map the other relevant columns. This is what I currently have:
function extractId() {
var responsesSheet = SpreadsheetApp.openById('XXXXXXX').getSheetByName('Sheet1');
var rosterSheet = SpreadsheetApp.openById('XXXXXXX').getSheetByName('Sheet1');
var responsesHighestEntry = responsesSheet.getLastRow();
var email;
var idNumber;
var rosterLastRow = rosterSheet.getLastRow();
var rosterArray = rosterSheet.getRange(2, 1, (rosterLastRow - 1)).getValues();
var studentResponsesRow;
var i;
var x;
var y = 2;
var fullName;
var grade;
var advisoryTeacher;
var advisoryRoom;
for (i = 2; i < responsesHighestEntry; i++) {
email = responsesSheet.getRange(i, 2).getValue();
idNumber = email.replace(/\@(.*)/i,"");
responsesSheet.getRange(i, 3).setValue(idNumber);
for (x = 0; x < rosterLastRow; x++) {
if (rosterArray[x] == idNumber) {
studentResponsesRow = y;
fullName = rosterSheet.getRange(y, 2).getValue();
responsesSheet.getRange(y, 4).setValue(fullName);
grade = rosterSheet.getRange(x + 2, 6).getValue();
responsesSheet.getRange(studentResponsesRow, 5).setValue(grade);
advisoryTeacher = rosterSheet.getRange(x + 2, 7).getValue();
responsesSheet.getRange(studentResponsesRow, 6).setValue(advisoryTeacher);
advisoryRoom = rosterSheet.getRange(x + 2, 8).getValue();
responsesSheet.getRange(studentResponsesRow, 7).setValue(advisoryRoom);
}
y++;
}
}
}
Sample roster sheet with demographic data.
Sample end-result responses sheet. In this case, the email addresses are auto-collected. Columns C-G would be the result of running the script.
回答1:
- You want to achieve "Sample end-result responses sheet" from the values of "roster sheet" in the images in your question.
- In your situation, "responses sheet" has only the column "B" which has the email addresses.
- You want to put the values that the ID from the email address is the same with the column "A" in "roster sheet".
- You want to achieve this using Google Apps Script.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
The flow of this modified script is as follows.
- Retrieve the values of the column "B" from "responses sheet".
- Retrieve the values from "roster sheet".
- Create an object for searching IDs.
- Create the result values as an array.
- Put the values to "responses sheet".
Modified script:
function extractId() {
var responsesSheet = SpreadsheetApp.openById('XXXXXXX').getSheetByName('Sheet1');
var rosterSheet = SpreadsheetApp.openById('XXXXXXX').getSheetByName('Sheet1');
var valuesOfresponsesSheet = responsesSheet.getRange(2, 2, responsesSheet.getLastRow() - 1, 1).getValues();
var valuesOfrosterSheet = rosterSheet.getRange(2, 1, rosterSheet.getLastRow() - 1, 8).getValues();
var obj = valuesOfrosterSheet.reduce(function(o, e) {
o[e[0]] = [e[0], e[1], e[5], e[6], e[7]];
return o;
}, {});
// var resultValues = valuesOfresponsesSheet.map(function(e) {return obj[e[0].replace(/\@(.*)/i,"")]});
var resultValues = valuesOfresponsesSheet.map(function(e) {return obj[e[0].toString().replace(/\@(.*)/i,"")] || ["","","","",""]}); // Modified
responsesSheet.getRange(2, 3, resultValues.length, resultValues[0].length).setValues(resultValues);
}
Reference:
- reduce()
来源:https://stackoverflow.com/questions/59419180/use-for-loop-to-map-data-from-one-google-sheet-to-another-one