如何在一封电子邮件 google 应用程序脚本中发送多张范围数据?
How to send multiple sheets range data in one email google app script?
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"});
}
}
}
};
您应该将邮件发送代码放在 for 语句之外,以便在完成所有 sheet 哦工作簿后发送邮件。
逻辑是
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"});
}
};
如果你想将两个工作表中的信息附加到“正文”和“主题”,你必须在循环工作表之前声明 var body 并在循环之后发送电子邮件。通过这种方式,您将从两个工作表中收集信息并将其分配给变量“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"});
}
};
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"});
}
}
}
};
您应该将邮件发送代码放在 for 语句之外,以便在完成所有 sheet 哦工作簿后发送邮件。
逻辑是
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"});
}
};
如果你想将两个工作表中的信息附加到“正文”和“主题”,你必须在循环工作表之前声明 var body 并在循环之后发送电子邮件。通过这种方式,您将从两个工作表中收集信息并将其分配给变量“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"});
}
};