google-apps-script - 消息错误:“无法读取 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>
解决方案
推荐阅读
- python - Python:如何在散景上添加多个补丁?
- c# - 如果我想稍后更改该字符串的值,但我想保留格式,如何将值插入字符串?
- progressive-web-apps - 有没有办法将 PWA/TWA 应用程序放在子域中
- javascript - 从 url 对点击 API 数据进行排序
- git - 永久删除 git 上特定分支的所有历史记录
- c++ - 按值捕获 shared_ptr 的 lambda 如何影响其 use_count()?
- perl - 使用 Perl 解析 Parquet 文件
- c - 当我们在 C 中覆盖联合字段时会发生什么?
- java - 使用jsoup解析html表格
- haskell - 有没有办法将这个函数重写为全部?