2015-01-21 2 views
0

reportData이라는 객체를 전달하는 showModalReport()이라는 Google Apps Script 함수가 있습니다.이 객체는 자바 스크립트 객체 유형 (속성 및 값 쌍이 있음)입니다. 이 기능은 생성하고 HtmlTemplate 제공 :HtmlService에서 Google Apps Script 함수로 변수 보내기

function showModalReport(reportData) { 

    var template = HtmlService.createTemplateFromFile('modalReport'); 

    template.reportData = reportData; // this makes reportData available in the HTML modal dialogue 

    template = template.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME); 

    SpreadsheetApp.getUi().showModalDialog(template, 'Test Report'); 

} 

그리고 여기 내 modalReport.html 파일입니다 :

<!DOCTYPE html> 

Report Name: <?= reportData.name ?> 

<input type="button" value="Email Report" onclick="onClickFunctions(???)" /> 

<script> 

function onClickFunctions(reportData) { 

    google.script.run.withSuccessHandler(function() { google.script.host.close(); }).emailReport(reportData); 

} 

</script> 
의 대화가 제대로 작동하고

보고서의 이름을 표시를, 그래서 나는 알고 그 reportData 변수를 사용할 수 있습니다. 다음으로 나는 사용자가 버튼을 클릭 할 때 emailReport() 함수를 호출하여 보고서 데이터를 다른 사용자에게 전자 메일로 보낸 다음 모달 대화 상자를 닫을 수 있도록하려고합니다. 여기

emailReport()입니다 :

function emailReport(reportData) { 

    Logger.log('reportData: ' + reportData); 

    // the email code 

} 

그러나 emailReport() 함수는 인수로 전달 된 reportData 개체가 필요하고, 이것이 내가에 붙어있어 일부입니다. reportDataemailReport()으로 전달하려면 어떻게해야하나요?

나는이 이미 시도 :

onclick="onClickFunctions(reportData)" // the Javascript console outputs the error: "Uncaught ReferenceError: reportData is not defined" 

onclick="onClickFunctions(<? reportData ?>)" // reportData gets sent in to the function but is null 

onclick="onClickFunctions(this.reportData)" // reportData gets sent in to the function but is null 

내가 함수에 그 변수를 보낼 수있는 방법에 대해 어떤 생각을?

또한 여기에는 내 것과 비슷한 다른 많은 질문이 있다는 것을 알고 있습니다. 나는 그들 중 많은 사람들로부터 코드를 읽고 시험해 보았을뿐만 아니라, 구글의 HTMLService 문서에서 추천 한 것을 시도해 보았다. 그러나 나는 아직도 붙어있다.

답변

1

당신은 할 수 JSON.stringify ReportData에 주위 패스 :

template.reportData = reportData; 
template.reportDataJSON = JSON.stringify(reportData); 

다음 :

onclick="onClickFunctions('<?= reportDataJSON ?>'); 

과 :

function emailReport(reportData) { 
    var reportData = JSON.parse(reportData); 
} 
+0

감사합니다! 또한이 문제를 접할 수있는 다른 사람들을 위해 : Cameron의 JSON 구문 분석 제안이 효과적인 해결책입니다. ''의 강제 인쇄 스크립틀릿 구문을 사용하는 첫 번째 제안은 'Uncaught SyntaxError : Unexpected identifier' 오류를 생성했습니다. – mike

관련 문제