Google apps script Google脚本自动填写Google文档(将文本字符串转换为货币)

Google apps script Google脚本自动填写Google文档(将文本字符串转换为货币),google-apps-script,Google Apps Script,第一次张贴,所以提前感谢 我有一个GoogleSheets,它有15到20个货币字段,我有一个脚本可以自动填充到GoogleDoc中。我感兴趣的代码片段是如何将{{Price1}}和{{Total1}}等标记转换为$#,####.00,而不是现在显示的## 我相信答案很简单,但我已经准备好了,我将竭尽全力想办法解决这个问题 //Start processing each spreadsheet row rows.forEach(function(row, index){ //Here we ch

第一次张贴,所以提前感谢

我有一个GoogleSheets,它有15到20个货币字段,我有一个脚本可以自动填充到GoogleDoc中。我感兴趣的代码片段是如何将{{Price1}}和{{Total1}}等标记转换为$#,####.00,而不是现在显示的##

我相信答案很简单,但我已经准备好了,我将竭尽全力想办法解决这个问题

//Start processing each spreadsheet row
rows.forEach(function(row, index){
//Here we check if this row is the headers, if so we skip it
if (index === 0) return;
//Here we check if a document has already been generated by looking at 'Document Link', if 
so we skip it
if (row[0]) return;
//Using the row data in a template literal, we make a copy of our template document in our 
destinationFolder
const copy = googleDocTemplate.makeCopy(`Invoice #:${row[2]}, ${row[1]} Invoice 1` , 
destinationFolder)
//Once we have the copy, we then open it using the DocumentApp
const doc = DocumentApp.openById(copy.getId())
//All of the content lives in the body, so we get that for editing
const body = doc.getBody();
//In this line we do some friendly date formatting, that may or may not work for you locale
const friendlyDate = new Date(row[3]).toLocaleDateString();



//In these lines, we replace our replacement tokens with values from our spreadsheet row
body.replaceText('{{Invoice Name}}', row[1]);
body.replaceText('{{Invoice}}', row[2]);
body.replaceText('{{Date}}', friendlyDate);
body.replaceText('{{Bill To}}', row[4]);
body.replaceText('{{Attn}}', row[5]);
body.replaceText('{{Bill Address}}', row[6]);
body.replaceText('{{Bill City}}', row[7]);
body.replaceText('{{Bill Telephone}}', row[8]);
body.replaceText('{{Project}}', row[9]);
body.replaceText('{{Project #}}', row[10]);
body.replaceText('{{Project Address}}', row[11]);
body.replaceText('{{Project City}}', row[12]);
body.replaceText('{{Project Telephone}}', row[13]);
body.replaceText('{{Desc1}}', row[14]);
body.replaceText('{{Qty1}}', row[15]);
    body.replaceText('{{Price1}}',Utilities.formatString("$%.2f",row[16]);
 body.replaceText('{{Total1}}', row[17]);
 body.replaceText('{{Desc2}}', row[18]);
 body.replaceText('{{Qty2}}', row[19]);
const url = doc.getUrl();
sheet.getRange(index + 1, 1).setValue(url)
 })

是否要将数字转换为自己的格式? 尝试替换:

body.replaceText('{{Price1}}',row[16]);
与:

对于美元,
“$%”.2f“
将起作用。 另见: ,

于2021年5月25日添加:


在这种情况下,我将创建一个新函数来实现这一点。它还使您的代码整洁。 比如:

myConvert(format, value){
  if (value == 0){ // or any appropriate condition
    return "null"; // or whatever
  }else{
    return Utilities.formatString(format, value)
  }
}

这只是我的猜测。尽管很遗憾,我看不到您的整个脚本和电子表格,例如,当您的电子表格的单元格类似于
$,####.00
并且您正在使用
设置值
检索值时,当您将
设置值
修改为
getDisplayValues
时,这是您期望的结果吗?如果我误解了你的情况,我道歉。非常感谢。最后一个问题…如何发送null值以使字段显示为null?例如,Price1可能是$12.20,但Price2可能是$0.00,因此我希望该字段显示为null,而不是在google文档上显示$0.00,因为google文档将是一个发票模板,我不希望到处都显示$0.00。如果我将值作为“”传递,我会得到以下错误:value.toFixed不是一个函数demuxes_uf@format\u string.js.gs:113 replacerDemuxer@format\u string.js.gs:212 formatString_@format\u string.js.gs:215在这种情况下,我将创建一个新函数来实现这一点。它还使您的代码整洁。类似于:`myConverrt(format,value){if(value==0){//或任何适当的条件返回“null”;//或任何}其他{return Utilities.formatString(format,value)}}`看起来我仍然在出错。我能问一下这个片段看起来是否正确吗?正文.replaceText('{Total2}}',第[21]行];body.replaceText({{Price3}},第[24]行;})}函数myConvert(format,value){if(value==0){//或任何适当的条件返回“”;//或任何其他{返回实用程序。formatString(format,value)const url=doc.getUrl();sheet.getRange(index+1,1)。setValue(url)}
myConvert(format, value){
  if (value == 0){ // or any appropriate condition
    return "null"; // or whatever
  }else{
    return Utilities.formatString(format, value)
  }
}