【问题标题】:Replacing a placeholder text label in a Google Docs table cell with a Google Drive image用 Google Drive 图片替换 Google Docs 表格单元格中的占位符文本标签
【发布时间】: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


    【解决方案1】:

    如果您想将图像插入到 Google Doc 中,您需要将其作为 blob 获取并调用 insertImage 函数,如下所示:

    var blob = DriveApp.getFileById(id).getBlob(); // Replace id with the ID of your icon file.
    
    // This is inserting it into a specific cell in an existing table.  
    var newImage = curTable.getRow(imgTargetRow).getCell(imgTargetCol).insertImage(0, blob);
    
      // This sets the image height and width based on my table cell sizes, but if your icon is sized exactly right you won't need to do this
      var h = newImage.getHeight();
      var w = newImage.getWidth();
      var w = ((w > h) ? 352 : 197); // Adjust for landscape or portrait images
      var h = 264;
      newImage.setHeight(h);
      newImage.setWidth(w);
    

    【讨论】:

    • 感谢您的回复@user2572362!但是有一个问题,我如何确保它替换模板中的特定占位符标签?例如,我想要一个特定的图像来替换一个名为 %image01% 的特定占位符标签。
    • 你可以试试这篇文章的功能。 stackoverflow.com/questions/20526566/…
    猜你喜欢
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-20
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多