使谷歌文档脚本适应谷歌表格



我有一个函数"myFunk((",在谷歌文档中运行良好。它基本上在工作表中搜索带有##的字段,并用用户输入替换它们。但是当我在更改函数后尝试在工作表中运行它时,我得到"在对象中找不到函数替换文本"。我错过了什么?

function myFunk() {
// Display a dialog box for each field you need information for.
var documentProperties = PropertiesService.getDocumentProperties();
var ui = SpreadsheetApp.getUi();
//var response = ui.prompt('Enter Name', 'Enter owners person's name', ui.ButtonSet.OK);
var nameResponse = ui.prompt("Enter the name of the document OWNER");
var salesperson = nameResponse.getResponseText();
var documentProperties = PropertiesService.getDocumentProperties();
var date = new Date();
var htmlDlg = HtmlService.createHtmlOutputFromFile("HTML_myHtml")
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setWidth(200)
.setHeight(150);
var modal = SpreadsheetApp.getUi();
modal.showModalDialog(htmlDlg, "Document Classification");
//Get Current Document ID
var documentId = SpreadsheetApp.getActiveSpreadsheet().getId();
console.log(documentId);
//Get the document body as a variable
var body = SpreadsheetApp.openById(documentId).getDataRange().getValues();
console.log(body);
//Insert the entries into the document
body.replaceText("##OWNER##", nameResponse.getResponseText());
//AddValuesFromModal();
}
<form id="docType">
<select id="selectDocumentType" name="documentClass">
<option value="PUBLIC">Public</option>
<option value="INTERNAL">Internal</option>
<option value="CONFIDENTIAL">Confidential</option>
<option value="SECRET">Secret</option>
</select>
<hr/>
<input type="button" onClick="formSubmit();" value="Submit" />
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>a
function formSubmit(){
Submit();
}


function Submit() {
var selectedValue = $('#selectDocumentType').val();
console.log(selectedValue);
google.script.run
.withSuccessHandler(closeIt)
.AddValuesFromModal(selectedValue);
};

function closeIt(){
google.script.host.close();
};
</script>

电子表格没有文档中的正文部分,而是具有范围,范围是表示您希望操作的电子表格中的单元格的数据网格。 您可以使用 Range 对象本身,而不是像在当前代码中那样尝试使用 Range 的值。

解决方案是使用 getDataRange(( 选择相关的范围,但不要调用 getValues((。 相反,请创建一个可以搜索和替换"范围"内数据的 TextFinder 对象。

它应该看起来像这样:

var range = SpreadsheetApp.openById(documentId).getDataRange();
var finder = range.createTextFinder('##OWNER##');
finder.replaceAllWith(nameResponse.getResponseText());

可以缩短为单行:

SpreadsheetApp.openById(documentId).getDataRange().createTextFinder('##OWNER##').replaceAllWith(nameResponse.getResponseText());

看:

https://developers.google.com/apps-script/reference/spreadsheet/range#createTextFinder(String(

https://developers.google.com/apps-script/reference/spreadsheet/text-finder#replaceWith(String(

最新更新