我希望在replace.bodytext字符串中使用一组'var'

问题描述 投票:0回答:1

我有一个单元格,名字和姓氏都在一起“Autumn Smith”

在我的代码自动填充的文档中,该名称被列出多次。我希望第一次提到名字时同时提到名字和姓氏,但随后在文档的其余部分中只提到名字 - “Autumn”

我有一个 var 设置为单独的名称,现在我想使用该 var 仅插入第一个名称。

这是我当前使用的 var 代码:

 var fullName = row[3];
 var bothNames = fullName.split(" ");
 var firstName = bothNames[0];
 var lastName = bothNames[1];
 var abbreviatedName = firstName.substring(0, 1) + ". " + lastName;

我的替换代码是:

body.replaceText(`{{Name1}}`, firstName (row[3]));

当我在 AppsScript 中运行代码时,不会生成任何错误。 当我尝试实际使用生成器时,我收到一条错误消息“firstName 不是函数”

我对编码非常陌生,不太确定我错过了什么

google-apps-script variable-assignment google-docs
1个回答
0
投票

名字是一个字符串而不是函数。你把它当作一个函数来使用

这对我有用

function myfunk() {
  const doc = DocumentApp.openById(gobj.globals.testdocid);
  const body = doc.getBody();
  const row = ["zero","one","two","Howard Hughs"]
  var fullName = row[3];
  var bothNames = fullName.split(" ");
  var firstName = bothNames[0];
  var lastName = bothNames[1];
  var abbreviatedName = firstName.slice(0, 1) + ". " + lastName;
  body.replaceText(`{{Name1}}`, abbreviatedName);
}
© www.soinside.com 2019 - 2024. All rights reserved.