我正在编写一个脚本,该脚本将从电子表格中获取数据并替换文档上的文本键,为电子表格上的每一行数据添加一个重复的页面到文档中。
当我使用虚拟数据创建一些测试数组时,该脚本工作正常,但当我尝试从电子表格中提取数据时它无法正常工作。
function requestGen3() {
var templateDocID = ScriptProperties.getProperty("backRxRequestDocID");
// go back to variable below after testing
//var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
var sheet = sheets[3];
var activeSheetName = sheet.getName();
var range = sheet.getRange(2, 1, sheet.getMaxRows() - 1, sheet.getMaxColumns());
var user = Session.getUser().getEmail();
///////////////////////////////////////////////////////////////////////////////////
// the problem seems to be somewhere between here....
///////////////////////////////////////////////////////////////////////////////////
// For every row of employee data, generate an employee object.
var ptObjects = getRowsData(sheet, range);
// Get document template, copy it as a new temp doc, and save the Doc’s id
var docID = DocsList.getFileById(templateDocID).makeCopy().getId();
var doc = DocumentApp.openById(docID);
var body = doc.getActiveSection();
var pars = doc.getParagraphs();
for( var i in pars ) //loop to keep a copy of the original paragraphs
pars[i] = pars[i].copy();
// Create an array for every row object
for (var i = 0; i < ptObjects.length; ++i) {
// Get a row object
var rowData = ptObjects[i];
for (var i = 0; i < rowData.length; ++i) {
// Replace place holder keys,
body.replaceText('%PHYS_NAME%', rowData.physName[i]);
body.replaceText('%PHYS_ADDR1%', rowData.physAddr1[i]);
body.replaceText('%PHYS_ADDR2%', rowData.physAddr2[i]);
body.replaceText('%PHYS_CITY%', rowData.physCity[i]);
body.replaceText('%PHYS_STATE%', rowData.physState[i]);
body.replaceText('%PHYS_ZIP%', rowData.physZip[i]);
body.replaceText('%PHYS_PHONE%', rowData.physPhone[i]);
body.replaceText('%PT_NAME%', rowData.firstName[i]);
body.replaceText('%PT_DOB%', rowData.ptDOB[i]);
if( i != physName.length-1 ) { //has next?
doc.appendPageBreak();
for( var j in pars )
doc.appendParagraph(pars[j].copy());
}
}
}
///////////////////////////////////////////////////////////////////////////////////
// ...... and here
///////////////////////////////////////////////////////////////////////////////////
// Save and close the document
doc.saveAndClose();
}
引用的其他函数来自“从电子表格读取数据”教程。当我调试脚本时, ptObjects和rowData变量似乎包含正确的信息,但它没有正确替换文档中的键(格式为 %KEY_NAME%)。我对此很陌生,所以可能会有一些明显的错误。
任何帮助将不胜感激