首页 > 解决方案 > 消息错误:“无法读取 nul 的属性 'getSheetByName' (ligne 4, fichier "code")

问题描述

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

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

你能帮我修复错误吗?

预先感谢您帮助我。

真挚地

B.Delcroix 来自法国图卢兹

我的程序,见下文。

谷歌应用脚​​本:

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

<!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>

标签: google-apps-script

解决方案


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

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

要将脚本绑定到电子表格:

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

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

SpreadsheetApp.getActiveSpreadsheet();

经过

SpreadsheetApp.openById(id)

其中id应替换为您要使用的电子表格的实际 ID - 否则脚本不知道您引用哪个电子表格。


推荐阅读