如何从谷歌电子表格中保留我的pdf生成文档中的主要零点?

时间:2014-08-26 17:14:41

标签: google-apps-script google-sheets google-form mailmerge

我创建了一个google测试表单和测试表单响应。在响应电子表格中,我写了下面的脚本来捕获来自>的输入值。生成pdf / doc>发送电子邮件至pdf& >将doc的副本保存在目标文件夹中。我遇到的问题是标记为密码的部分可能以“0”或多个“0”开头,我已在电子表格的脚本中对其进行了格式化。但是,当它生成PDF / DOC时,零值将被删除。在PDF / DOC中转换时,有没有办法保持前导0?

function formatNumber(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("D2:D");
// Always show 4 digits
 cell.setNumberFormat("0000");
}
var docTemplate = "11laarf0ThJ4mANX4KzHZCblVwRgphlf-bCblaUMl4oc";
var docName = "Test Form";
function onFormSubmit(e) {
var email_address = "jimmy111@gmail.com";
var Time_Stamp = e.namedValues["Timestamp"];
var full_name = e.namedValues["Name"];
var phone = e.namedValues["Phone Number"];
var passcode = e.namedValues["Passcode"];
var price = e.namedValues["Price"];
var copyId = DocsList.getFileById(docTemplate)
.makeCopy(full_name+' '+docName)
.getId();
var copyDoc = DocumentApp.openById(copyId);
  var copyBody = copyDoc.getActiveSection();
  copyBody.replaceText('keyDate', Time_Stamp);
  copyBody.replaceText('keyName', full_name);
  copyBody.replaceText('keyPhone', phone);
  copyBody.replaceText('keyCode', passcode);
  copyBody.replaceText('keyPrice', price);
  copyDoc.saveAndClose();
  var pdf = DocsList.getFileById(copyId).getAs("application/pdf");
  var subject = "CIR Testform for "+ full_name + "";
  var body = "Here is the registration form for "+ full_name +"";
  MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf});
  var targetFolder = DocsList.getFolderById('0B-LfisIjjXtvLW82X3o1UzNyY1U');
  var file = DocsList.getFileById(copyId);
  file.addToFolder(targetFolder);
  file.removeFromFolder(DocsList.getRootFolder());
}

function assignEditUrls() {
  var form = FormApp.openById('16zVePLm61yRsSZMaoDI5MWIeB-vGHZGEoR9J7uJ0CP8');
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses 1');
  var data = sheet.getDataRange().getValues();
  var urlCol = 6; // column number where URL's should be populated; A = 1, B = 2 etc
  var responses = form.getResponses();
  var timestamps = [], urls = [], resultUrls = [];

  for (var i = 0; i < responses.length; i++) {
    timestamps.push(responses[i].getTimestamp().setMilliseconds(0));
    urls.push(responses[i].getEditResponseUrl());
  }
  for (var j = 1; j < data.length; j++) {

    resultUrls.push([data[j][0]?urls[timestamps.indexOf(data[j] [0].setMilliseconds(0))]:'']);
  }
  sheet.getRange(2, urlCol, resultUrls.length).setValues(resultUrls);  
}

1 个答案:

答案 0 :(得分:0)

我已经找到了解决这个问题的方法,但它并不理想,但它正在发挥作用。

使用预填表格功能,在每个需要前导零的响应中输入句点。

因此,如果您的用户输入了&#34; 0123456&#34;,则记录的响应将显示为&#34; 0.123456&#34;然后,您可以使用子字符串删除前两个字符。

e.g。

var phone = e.namedValues["Phone Number"].substring(2);

这将为您提供前导零。唯一的问题是从预先填写的表单中意外删除了期间,尽管您可以设置验证来检查它,然后发出错误,要求用户输入它,如果他们已将其删除。

不漂亮,但我们现在至少已经开始运行了。