도움을 주셔서 감사합니다.Google 시트 스크립트 - 셀 값을 찾으면 헤더 값 반환
첫 번째 행에 헤더 값이 포함 된 Google 시트가 있습니다. 나는 시트의 나머지 부분 (행 단위)을보고있는 스크립트를 가지고 있으며 셀이 특정 색상이라면 스크립트는 카운트를 유지합니다. 결국 카운트 수가 시트에 설정된 변수보다 크면 스크립트가 전자 메일을 트리거합니다.
스크립트에서 설정된 색상의 셀을 찾으면 열 머리글 값을 캡처하는 것이 무엇입니까? 나는 헤더 값을 가진 배열을 생성하고 위치를 비교할 필요가 있다고 확신한다. 그렇게 효율적으로하는 방법을 모르겠다.
function sendEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = ss.getSheets()[0];
var lastRow = dataSheet.getLastRow();
var lastColumn = dataSheet.getLastColumn();
//Project Range Information
var projectRange = dataSheet.getRange(6,3,lastRow-5,lastColumn);
var projectRangeValues = projectRange.getValues()[0];
var cellColors = projectRange.getBackgrounds();
//Student Information Range
var studentRange = dataSheet.getRange(6,1,lastRow-5,lastColumn);
var studentRangeValues = studentRange.getValues();
//Pull email template information
var emailSubject = ss.getRange("Variables!B1").getValue();
var emailText = ss.getRange("Variables!B2").getValue();
var triggerValue = ss.getRange("Variables!B4").getValue();
var ccValue = ss.getRange("Variables!B5").getValue();
//Where to Start and What to Check
var colorY = ss.getRange("Variables!B6").getValue();
var count = 0;
var startRow = 6;
//Loop through sheet and pull data
for(var i = 0; i < cellColors.length; i++) {
//Pull some information from the rows to use in email
var studentName = studentRangeValues[i][0];
var studentBlogUrl = studentRangeValues[i][1];
var studentEmail = studentRangeValues[i][2];
var studentData = [studentName,studentBlogUrl];
//Loop through cell colors and count them
for(var j = 0; j < cellColors[0].length ; j++) {
if(cellColors[i][j] == colorY) {
/*This is where I feel I need to add the array comparisons to get the header values */
count = count + 1;
};//end if statement
};//end for each cell in a row
//If the count is greater than trigger, send emails
if (count >= triggerValue) {
//A call to another function that merges the information
var emailBody = fillInTemplateFromObject(emailText, studentData);
MailApp.sendEmail({
to: studentEmail,
cc: ccValue,
subject: emailSubject,
htmlBody: emailBody,
});
} else {};
//reset count to 0 before next row
count = 0;
};//end for each row
};
편집 : 나는 응답을 기반으로 위의 코드 섹션을 업데이트 한 :
//Loop through cell colors and count them
for(var j = 0; j < cellColors[0].length ; j++) {
if(cellColors[i][j] == colorY) {
//This pushes the correct information into the array that matches up with the columns with a color.
missingAssignments.push(headervalues[i][j]);
count = count + 1;
};//end if statement
};//end for each cell in a row
내가 문제를 :에서
//Header Information
var headers = dataSheet.getRange(4,4,1,lastColumn);
var headerValues = headers.getValues();
var missingAssignments = new Array();
내가 추가 한 루프의 실행 중 오류가 발생했습니다 - TypeError : 정의되지 않은 "2"속성을 읽을 수 없습니다. 이것은 스크립트가 다음 행으로 이동함에 따라 for 루프를 밀어 넣음으로써 발생합니다. 왜이 오류가 발생하는지 확신 할 수 없습니다. 읽은 다른 것들로부터 배열은 정의되지 않은 것으로 설정됩니다. 내가 배열을 비우려고 노력하고 그것의 길이를 0으로 설정했지만 도움이되지 않습니다. 나는 그것이 끝날 때 배열의 유효 범위를 이해하지 못한다고 생각합니다.
편집 : "i"가 반복해서는 안됩니다. 읽어야합니다 :
missingAssignments.push(headervalues[0][j]);
첫 번째 for 루프의 끝 나는 다음 행의 배열을 지 웁니다.
missingAssignments.length = 0;
안녕하세요, 귀하의 답변에 감사드립니다. 그것은 옳은 길로 나를 잡았고 나의 헤더가 1 번 행에 있다면 이것이 완벽 할 것입니다. 시트를 업데이트해야하고 필요한 헤더 정보가 다른 행으로 끝납니다. 나는 몇몇 코드를 업데이트했다. 왜 내가 TypeError를 얻었을지 안다면 궁금해 하던가 ?? –
알았어 두 번째 변수에 iterating했다. –
필자는 완전히 확신 할 수는 없지만 getValues에서 반환되는 값은 항상 다차원 배열이기 때문에 그럴 것이라고 생각합니다. 다음과 같이해야합니다. var headerValues = headers.getValues () [0]; –