【发布时间】:2016-06-14 08:19:02
【问题描述】:
我正在尝试仅使用 Google Apps 自动填充报告。顺序如下:
Google 表单 > Google 表格 > Google 文档模板
Google 表单用于输入最终报告的所有详细信息:质量得分(1、2 或 3)和一些文本。在 Google 表格中,质量得分会转换为基于文本的标签(#good#、#ok#、#bad#)。
我在 Stack Overflow 上找到了一个脚本,该脚本选取了一个 Google 文档模板并使用 Google 表格中的值填充占位符标签 (%placeholder%)。但是,我无法解决用 Google Drive 中的图像/图标(绿色、橙色和红色图标)替换质量得分标签的问题。我将标签替换为图标的文件名(例如 icon-green.png),而不是图标本身。值得一提的是,质量得分标签放置在表格单元格中。
我对 javascript 非常陌生,真的可以使用您的帮助!我已经在下面的代码中指出了我被卡住的确切位置(接近尾声)。
// Replace this with ID of your template document.
var TEMPLATE_ID = '1AatkH57Iq3FwmsvZkCMYddjACItWS_XuO9sCsfcaMso'
// You can specify a name for the new PDF file here, or leave empty to use the
// name of the template.
var DOC_FILE_NAME = 'First GA Doc Template Try'
/**
* Eventhandler for spreadsheet opening - add a menu.
*/
function onOpen() {
SpreadsheetApp
.getUi()
.createMenu('Create Doc')
.addItem('Create Doc', 'createDoc')
.addToUi()
} // onOpen()
/**
* Take the fields from the active row in the active sheet
* and, using a Google Doc template, create a Doc with these
* fields replacing the keys in the template. The keys are identified
* by having a % either side, e.g. %Name%.
*
* @return {Object} the completed Doc file
*/
function createDoc() {
if (TEMPLATE_ID === '') {
SpreadsheetApp.getUi().alert('TEMPLATE_ID needs to be defined in code.gs')
return
}
// Set up the docs and the spreadsheet access
var copyFile = DriveApp.getFileById(TEMPLATE_ID).makeCopy(),
iconGreen = DriveApp.getFileById('0B0C0sFLzFLyWOUVSeWxQa2o2MFE'),
copyId = copyFile.getId(),
copyDoc = DocumentApp.openById(copyId),
copyBody = copyDoc.getActiveSection(),
activeSheet = SpreadsheetApp.getActiveSheet(),
numberOfColumns = activeSheet.getLastColumn(),
activeRowIndex = activeSheet.getActiveRange().getRowIndex(),
activeRow = activeSheet.getRange(activeRowIndex, 1, 1, numberOfColumns).getValues(),
headerRow = activeSheet.getRange(1, 1, 1, numberOfColumns).getValues(),
columnIndex = 0
// Replace the keys with the spreadsheet values
for (;columnIndex < headerRow[0].length; columnIndex++) {
copyBody.replaceText('%' + headerRow[0][columnIndex] + '%',
activeRow[0][columnIndex])
}
copyBody.replaceText('#good#', iconGreen) // This is where I'm stuck..
copyFile.setTrashed(false)
SpreadsheetApp.getUi().alert('New Doc created in the root of your Google Drive')
} // createDoc()
【问题讨论】:
标签: javascript google-apps-script google-docs