Google云端硬盘文件夹中有两个电子表格。如何在一封电子邮件中发送多个工作表中的数据
到目前为止,当我执行脚本时,它将发送3封电子邮件,因为它包含两个工作簿中的3张纸。一本工作簿包含两页。我希望将两张纸通过一封电子邮件发送。现在,它正在发送两个单独的电子邮件。
function getAllSheets(){
var file, files =
DriveApp.getFolderById("1JTtvZNc41D575Ubu3NbzVYTh9tX8KhGj").getFiles();
while (files.hasNext()) {
file = files.next();
var activeSpreadSheet = SpreadsheetApp.open(file);
var sheets = activeSpreadSheet.getSheets();
//loop through sheets to look for value
for (var i in sheets) {
var sheet = sheets[i]
var data = sheet.getDataRange().getValues();
var resultArr=[];
var xTitle = 'Part Numbers'; // XAxis Title
var yTitle = 'Quantity'; // YAxis Title
//To Loop through the whole data Rows
for(var i=1;i<data.length;i++)
{
//Takes columns from L to S (To loop through the Columns)
for(var j=11;j<19;j++)
{
var cellVal=data[i][j];
Logger.log(cellVal)
if(cellVal>0)
{
//Stores the Part No, Month Header Value of the Column, Cell
Value which is greater then 0
resultArr.push([data[i][0],data[0][j],cellVal])
}
}
}
if(resultArr.length>0)
{
var subject = 'Range exceeded Alert' + "" + sheet.getName();
//Creates a body through the obtained values
var body='';
for(var m=0;m<resultArr.length;m++)
{
body+="For Part No "+resultArr[m][0].toString()+" and Month
"+resultArr[m][1]
.toString()+", Value is "+resultArr[m][2].toString()+"<br>";
}
//send email
MailApp.sendEmail({to:"foo@bar.com",subject:subject,
htmlBody:body
+ " Chart! <br> <img src='cid:chartImg'> ! <br> Done"});
}
}
}
};
答案 0 :(得分:2)
您应该在for语句之外使用邮件发送代码,以便在您遍历所有工作表或工作簿后发送邮件。
逻辑将是
Function{
Get files
Through files{
Get sheets
Through sheets{
Get Data
Write data in body
}
Send mail with data
}
}
它看起来像(未经测试):
function getAllSheets(){
var file, files =
DriveApp.getFolderById("1JTtvZNc41D575Ubu3NbzVYTh9tX8KhGj").getFiles();
while (files.hasNext()) {
file = files.next();
var activeSpreadSheet = SpreadsheetApp.open(file);
var sheets = activeSpreadSheet.getSheets();
var body='';
//loop through sheets to look for value
for (var i in sheets) {
var sheet = sheets[i]
var data = sheet.getDataRange().getValues();
var resultArr=[];
var xTitle = 'Part Numbers'; // XAxis Title
var yTitle = 'Quantity'; // YAxis Title
//To Loop through the whole data Rows
for(var i=1;i<data.length;i++){
//Takes columns from L to S (To loop through the Columns)
for(var j=11;j<19;j++){
var cellVal=data[i][j];
Logger.log(cellVal)
if(cellVal>0){
//Stores the Part No, Month Header Value of the Column, Cell
Value which is greater then 0
resultArr.push([data[i][0],data[0][j],cellVal])
}
}
}
if(resultArr.length>0){
var subject = 'Range exceeded Alert' + "" + sheet.getName();
//Creates a body through the obtained values
for(var m=0;m<resultArr.length;m++){
body+="For Part No "+resultArr[m][0].toString()+" and Month
"+resultArr[m][1]
.toString()+", Value is "+resultArr[m][2].toString()+"<br>";
}
}
}
//send email
MailApp.sendEmail({to:"sriram6897@gmail.com",subject:subject,
htmlBody:body
+ " Chart! <br> <img src='cid:chartImg'> ! <br> Done"});
}
};
答案 1 :(得分:1)
如果要将两个工作表的信息都附加到“ body”和“ subject”,则必须在循环浏览工作表之前声明var body,然后发送AFTER循环电子邮件。这样,您将从两个工作表中收集信息,并将其分配给变量“ body”和“ subject”。
我测试了与Pierre-Marie Richard相似的代码,同时解决了“主题”问题。在此处将全部有效载荷添加到可见性中,最终结果:
function getAllSheets(){
var file, files = DriveApp.getFolderById("1JTtvZNc41D575Ubu3NbzVYTh9tX8KhGj").getFiles();
while (files.hasNext()) {
file = files.next();
var URL=file.getUrl()
var parent=file.getParents()
Logger.log(parent)
var activeSpreadSheet = SpreadsheetApp.open(file);
var sheets = activeSpreadSheet.getSheets();
// define body and subject before looping though the sheets, like this information of both sheets will be attached to the body
var body='';
var subject = 'Range exceeded Alert';
//loop through sheets to look for value
for (var i in sheets) {
var sheet = sheets[i]
var data = sheet.getDataRange().getValues();
var resultArr=[];
var xTitle = 'Part Numbers'; // XAxis Title
var yTitle = 'Quantity'; // YAxis Title
//To Loop through the whole data Rows
for(var i=1;i<data.length;i++)
{
//Takes columns from L to S (To loop through the Columns)
for(var j=11;j<19;j++)
{
var cellVal=data[i][j];
Logger.log(cellVal)
if(cellVal>0)
{
//Stores the Part No, Month Header Value of the Column, Cell Value which is greater then 0
resultArr.push([data[i][0],data[0][j],cellVal])
}
}
}
if(resultArr.length>0)
{
// add the subject of each sheet here
subject+= " "+'Range exceeded Alert' + "" + sheet.getName();
//Creates a body through the obtained values
for(var m=0;m<resultArr.length;m++)
{
body+="For Part No "+resultArr[m][0].toString()+" and Month"+resultArr[m][1]
.toString()+", Value is "+resultArr[m][2].toString()+"<br>";
}
}
}
//send email after looping through the sheets, so that information of both sheets will be sent
MailApp.sendEmail({to:"your email",subject:subject,
htmlBody:body
+ " Chart! <br> <img src='cid:chartImg'> ! <br> Done"});
}
};