首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >消息错误:“无法读取nul的属性'getSheetByName‘(Lenge4,虚构的”代码“)

消息错误:“无法读取nul的属性'getSheetByName‘(Lenge4,虚构的”代码“)
EN

Stack Overflow用户
提问于 2020-07-03 12:59:25
回答 1查看 153关注 0票数 0

首先,这是我第一次把关于电子签名的工作放在GMAIL ( html)中。我创建了简单的html并将其放入gmail中。它工作得很好

现在,我的目标是提取像姓氏、名字和职业(作为帐户google的功能)的数据,将它们放在HTML中,然后在gmail中(签名参数)。由于我对javascript等一无所知.,我在youtube上跟踪了解它是如何工作的(参见链接:"")。我收到一条错误消息“无法读取属性(getSheetByName 'of null”)。在我看来,表是空的。所以,我为空的情况添加了一个"if“部分。它仍然不能工作。

你能帮我修一下错误吗?

请提前感谢您的帮助。

由衷地

来自法国图卢兹的B.Delcroix

我的程序,见下文.

谷歌应用程序脚本:

代码语言:javascript
复制
function myFunction() {

  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const ws = ss.getSheetByName('benevoles');
  if (ws != null) {
  Logger.log(ws.getIndex());
}
 const h1 = ws.getRange("B3").getValue();
  const subheader = ws.getRange("B4").getValue();
  const headers = ws.getRange("B6;E6").getValues();
  const initiale = headers[0][0];
  const name = headers[0][1];
  const prenom = headers[0][2];
  const profession = headers[0][3];

  const lr = ws.getLastRow();
  const tableRangeValues = ws.getRange(7,2,lr-7,4).getValues();

  const totalLine = ws.getRange(lr,2,1,4).getValues();
  const totalTexte = totalLine[0][0];
  const totalCount = totalLine[0][3];

  const htmlTemplate = HtmlService.createTemplateFromFile("mail");

  htmlTemplate.h1 = h1;

   htmlTemplate.subheader  = subheader ;
   htmlTemplate.headers = headers;
   htmlTemplate.initiale = initiale;
   htmlTemplate.name = name;
  
   htmlTemplate.totalTexte  = totalTexte ;
   htmlTemplate.totalCount = totalCount;
   htmlTemplate.tableRangeValues  = tableRangeValues ;

  const htmlForEmail = htmlTemplate.evaluate().getContent();

  console.log(htmlForEmail);

  GmailApp.SendEmail(
     "xxxxxxx@xxxx.com",
     "test envoi mail html",
     "svp, ouvrez ce mail avec ce support client HTML",
     {htmlbody : htmlForEmail});
}

HTML

代码语言:javascript
复制
<!DOC TYPE html>
<html>
  <Head>
    <base target="_top">
  </Head>
  <body>
    <div><div>
       </div>
       <div>
          <h1> <?= h1 ?></h1>
          <div> <?= subheader ?></div>
          <div></div>
           <Table>
           <Thead>
           <tr><th>initiale</th>
              <th>name</th>
              <th>prenom</th>
              <th>profession</th></tr>
           </thead>
           <tbody>
             <tr>
              <td>
              Col1 d1
              </td>
              <td>
              Col2 d2
              </td>
              <td>
              Col3 d3
              </td>
           </tr>
           </tbody>
           <tfoot>
             <tr>
              <td>
              <?= totalTexte ?>
              </td>
              <td>
           
              </td>
              <td>
              <?= totalCount ?>
              </td>
           </tr>
           </tfoot>
           </Table>
       </div>
       <div>
       </div>
    </div>
  </body>
</html>
EN

回答 1

Stack Overflow用户

发布于 2020-07-03 13:50:38

您的错误意味着您使用的是独立脚本而不是容器绑定脚本

您正在使用SpreadsheetApp.getActiveSpreadsheet();方法,但是如果您的脚本没有绑定到任何电子表格,则没有活动的电子表格!

将脚本绑定到电子表格:

  • 打开感兴趣的电子表格
  • 继续工具->脚本编辑器
  • 粘贴脚本并保存它

或者,如果您更喜欢使用独立脚本,则替换行

SpreadsheetApp.getActiveSpreadsheet();

通过

SpreadsheetApp.openById(id)

因此,应该将id替换为要使用的电子表格的实际Id --否则脚本不知道您引用的是哪个电子表格。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62715981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档