2012-10-18 10 views
1

저는 자바 스크립트에 대해 매우 익숙하며 Google 스프레드 시트와 메일에서 편지 병합 기능을 사용하려고합니다. 튜토리얼 스크립트를 복사하고 몇 가지 필요한 변경을했습니다 (적어도 내가 생각할 수있는 것). 하지만 스크립트를 실행하려고하면 TypeError가 발생합니다. null에서 "length"속성을 읽을 수 없습니다. (43 행)TypeError : null에서 "length"속성을 읽을 수 없습니다.

위의 43 행은 for 루프입니다. 누군가가 스크립트를 실행할 수 있도록 수정해야 할 부분을 알려주는 데 도움을 줄 수 있습니까?

// Replaces markers in a template string with values define in a JavaScript data object. 
// Arguments: 
// - template: string containing markers, for instance ${"Column name"} 
// - data: JavaScript object with values to that will replace markers. For instance 
//  data.columnName will replace marker ${"Column name"} 
// Returns a string without markers. If no data is found to replace a marker, it is 
// simply removed. 
function fillInTemplateFromObject(template, data) { 
    var email = template; 
    // Search for all the variables to be replaced, for instance ${"Column name"} 
    var templateVars = template.match(/\$\{\"[^\"]+\"\}/g); 
    // Replace variables from the template with the actual values from the data object. 
    // If no value is available, replace with the empty string. 
    for (var i = 0; i < templateVars.length; ++i) { 
     // normalizeHeader ignores ${"} so we can call it directly here. 
     var variableData = data[normalizeHeader(templateVars[i])]; 
     email = email.replace(templateVars[i], variableData || ""); 
    } 
    return email; 
} 

답변

0

정규 표현식과 일치하는 것이 없으면 templateVars은 null입니다. 루프 전에 이것을 확인해야합니다.

업데이트 : 난 그냥이 같은 문제를 가지고 있지만, 영업 이익은 내가 생각하지 않는 데 문제가 코드에 관련 하나입니다

if (templateVars !== null) { 
    for (var i = 0; i < templateVars.length; i++) { 
     ... 
    } 
} 
+0

음 ... 죄송합니다. 자바 스크립트에 대해 더 많이 알고 싶지만 그렇지 않습니다. 나는 그것을 어떻게 확인하는지 잘 모른다. – user1755506

+0

'for' 루프를 작성하는 방법을 알고 있지만'if' 구문은 작성하지 않았습니까? 업데이트 된 답변보기 – Barmar

+0

Google doc에서 스크립트를 가져 왔습니다. :) 내 요구 사항을 충족하기 위해 조정할 필요가 있습니다. 문제가 발생했습니다. 죄송합니다 ... – user1755506

0

.

자습서에서 Google이 제공하는 이메일 템플릿의 형식입니다.

자리 표시 ${"First Name"} 그러나 (다른 하나는 "이탤릭체", 당신은

차이가 하나입니다 수직 (작품) 대 "입니다 전혀 다르다 ${“First Name”}를 얻을 수 있습니다 당신은이를 편집하는 방법에 따라한다 does not work)

컴퓨터 포맷 데이터를 알고있는 사람들은이 코드의 중요성을 설명 할 수 있지만 간단히 코드를 중단합니다.

+0

스마트 쿼트라고합니다. – Unihedron

관련 문제