问题描述
这是我的代码,我无法弄清楚为什么replaceText()不起作用。
函数createDoc(){
var templateid =1jM-6Qvy47gQ45u88WfDU_RvfuSTsw27zBP_9MfsUGr8; //获取模板文件ID
var FOLDER_NAME =已完成租赁协议; //文件夹的名称doc
//从单个用户获取数据
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var data = sheet.getRange(2,1,sheet.getLastRow() - 1,sheet.getLastColumn())。getValues();
var lastRow = sheet.getLastRow();
var firstName = sheet.getRange(lastRow,2,1,1).getValues();
var lastName = sheet.getRange(lastRow,3,1,1).getValues();
var guestEmail = sheet.getRange(lastRow,7,1,1).getValues();
var companyEmail [email protected];
var companyName =熊湖项目管理;
var username [email protected]; //获取他们的电子邮件(从表名)
var me [email protected];
$ b $ //复制模板
var docid = DocsList.getFileById(templateid).makeCopy(Rental Agreement - + firstName ++ lastName + - + guestEmail).getId();
// var file = DocsList.getFileById(docid).addEditors(me);
//将文件移动到正确的文件夹
var file = DocsList.getFileById(docid);
var folder = DocsList.getFolder(FOLDER_NAME);
file.addToFolder(folder);
var doc = DocumentApp.openById(docid);
var body = doc.getActiveSection();
var body_text = doc.addEditor([email protected]);
//追加小屋规则
// doc.appendParagraph(这是一个典型的段落。);
body.replaceText(/ ^ companyEmail $ /,test);
body.replaceText(%companyName%,test1);
body.replaceText(%todayDate%,test1);
doc.saveAndClose();
}
.replaceText和body.replaceText以及其他几个选项。
任何想法为什么这是行不通的?
提前致谢
body.replaceText(%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% companyName%,test1);
这将在每一个companyName的实例上查找%。这种情况下的%就是这样,一个标点符号在一个陌生的地方。这是一个惯例,用于降低意外替换文档中真实文本的可能性。
您的模板文档必须具有确切的替换模式。 (你的不是......而是你只有companyName,将它改为%companyName%。)将这个规则应用于你想要做的任何其他替换。
您可以从一些优化中受益。
...
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//下一行很难维护 - 有更好的方法。
// var data = sheet.getRange(2,1,sheet.getLastRow() - 1,sheet.getLastColumn())。getValues();
//读取整个电子表格,跳过标题
var data = sheet.getDataRange()。getValues()。slice(1);
//已经读取所有数据,使用它而不是再次读取表单。
var firstName = data [data.length-1] [2-1]; //(2-1)因为数组从0开始计数
var lastName = data [data.length-1] [3-1]; //电子表格列1
var guestEmail = data [data.length-1] [7-1]; //更好:将这些变量放入变量中。
...
在尝试你的代码,我遇到了一个自动完成问题,使用 doc.getActiveSection()
。根据4月15日的。
$ b
您应该相应地更新您的代码。
Here is my code and I can't figure out why replaceText() isn't working.
function createDoc(){
var templateid = "1jM-6Qvy47gQ45u88WfDU_RvfuSTsw27zBP_9MfsUGr8"; // get template file id
var FOLDER_NAME = "Completed Rental Agreements"; // folder name of where to put doc
// get the data from an individual user
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var data = sheet.getRange(2, 1, sheet.getLastRow()-1,sheet.getLastColumn()).getValues();
var lastRow = sheet.getLastRow();
var firstName = sheet.getRange(lastRow, 2, 1,1).getValues();
var lastName = sheet.getRange(lastRow, 3, 1,1).getValues();
var guestEmail = sheet.getRange(lastRow, 7, 1,1).getValues();
var companyEmail = "[email protected]";
var companyName = "Bear Lake Project Management";
var username = "[email protected]"; // get their email (from sheet name)
var me = "[email protected]";
//Copy Template
var docid = DocsList.getFileById(templateid).makeCopy("Rental Agreement - "+firstName+""+lastName+"-"+guestEmail).getId();
// var file = DocsList.getFileById(docid).addEditors(me);
// move file to right folder
var file = DocsList.getFileById(docid);
var folder = DocsList.getFolder(FOLDER_NAME);
file.addToFolder(folder);
var doc = DocumentApp.openById(docid);
var body = doc.getActiveSection();
var body_text = doc.addEditor("[email protected]");
// Append Cabin Rules
// doc.appendParagraph("This is a typical paragraph.");
body.replaceText("/^companyEmail$/", "test");
body.replaceText("%companyName%", "test1");
body.replaceText("%todayDate%", "test1");
doc.saveAndClose();
}
I've tried doc.replaceText and body.replaceText along with several other options.
Any ideas why this isn't working?
Thank in advance
Consider this:
body.replaceText("%companyName%", "test1");
That will look for every instance of "companyName" with "%" on either side of it. The "%" in this case is just that, a piece of punctuation in a strange place. This is a convention used to decrease the likelihood of accidentally replacing real text in the document.
Your template document must have that exact pattern for the replacement to work. (Yours doesn't... instead you have just "companyName". Change it to "%companyName%".) Apply that rule for any other replacement you want to make.
You could benefit from some optimization.
...
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
// Next line is hard to maintain - there's a better way.
// var data = sheet.getRange(2, 1, sheet.getLastRow()-1,sheet.getLastColumn()).getValues();
// Read whole spreadsheet, skip headers
var data = sheet.getDataRange().getValues().slice(1);
// Already read in all data, use it instead of reading sheet again.
var firstName = data[data.length-1][2-1]; // (2-1) because array counts from 0
var lastName = data[data.length-1][3-1]; // while spreadsheet columns from 1
var guestEmail = data[data.length-1][7-1]; // Better: put these into variables.
...
While experimenting with your code, I ran into an autocompletion issue with doc.getActiveSection()
. It turns out that there has been a recent change, according to the release notes for April 15 2013.
You should update your code accordingly.
这篇关于Google Apps脚本文档无法通过replaceText()访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!