NOT A CODER - 通过小脚本/代码实现机会主义的吝啬。我目前有一个谷歌电子表格连接到谷歌表格。它包含四个标签,即ChangeLog
,Secondary
,form responses 1
(隐藏),send log
(隐藏):
ChangeLog :是一个使用查询的标签(链接到表单响应1)和格式化以拉取原始数据,以某种方式查看。是个 '活动'片
辅助:使用查询(链接到表单响应1)的标签是否仅显示选择列
表单回复1 :隐藏;从谷歌表格中获取原始信息。
发送日志:隐藏;来自插件;未使用/重要
我有一个脚本,每天将整个电子表格发送给特定的人;借用了一个足以在网上发布剧本的人。我想写一个第二个脚本文件,它只会发送' Secondary'工作表/标签输出(不共享受保护的信息),与当前脚本相同的计划。我一直在寻找并使用我的剧本,但只是出错或者我仍然可以获得所有标签。
这是我目前的工作,并将其发送给所有人'脚本...我可以添加什么来限制发送到ONE SHEET / TAB的内容?我甚至无法隐藏其他标签。
function sendEmail() {
var ssID = SpreadsheetApp.getActiveSpreadsheet().getId();
var sheetName = SpreadsheetApp.getActiveSpreadsheet().getName();
//var email = Session.getUser().getEmail();
var email_ID1 = "email1@co.com";
var email_ID2 = "email2@co.com";
var email_ID3 = "email3@co.com";
var subject = "Change Notice Log";
var body = "Attached is the Change Notice Log. You will be asked if you trust this attachment, please select 'Yes' at the prompt. This daily report will capture all submittals, not just the day sent, so please refer to the timestamp column for the most recent date. This log is scheduled to automatically send on a daily basis, regardless of new submissions. If you have any questions, please contact HR Team. Thanks! ";
var requestData = {"method": "GET", "headers":{"Authorization":"Bearer "+ScriptApp.getOAuthToken()}};
var url = "https://docs.google.com/spreadsheets/d/"+ ssID + "/export?format=xlsx&id="+ssID;
var result = UrlFetchApp.fetch(url , requestData);
var contents = result.getContent();
MailApp.sendEmail(email_ID1,subject ,body, {attachments:[{fileName:sheetName+".xls", content:contents, mimeType:"application//xls"}]});
MailApp.sendEmail(email_ID2,subject ,body, {attachments:[{fileName:sheetName+".xls", content:contents, mimeType:"application//xls"}]});
MailApp.sendEmail(email_ID3,subject ,body, {attachments:[{fileName:sheetName+".xls", content:contents, mimeType:"application//xls"}]});
};
如果有人提出要求,我道歉在某个地方回答,我滚动了很多,无法让任何其他建议对我有用。我感谢任何人都能提供的任何帮助。
答案 0 :(得分:2)
要附加特定工作表,您需要在网址中包含工作表ID here。基本上,您的网址结构将如下所示:
https://docs.google.com/spreadsheets/d/KEY/export?format=csv&id=KEY&gid=SHEET_ID
其中Key是您的电子表格ID,Sheet_ID是您要导出的工作表的ID
要以编程方式获取Sheet ID,您可以使用此类函数
function getSheetID(name){
var ss = SpreadsheetApp.getActive().getSheetByName(name)
var sheetID = ss.getSheetId().toString()
return sheetID
}
您最终的代码如下:
function sendEmail() {
var ssID = SpreadsheetApp.getActiveSpreadsheet().getId();
var sheetName = SpreadsheetApp.getActiveSpreadsheet().getName();
//var email = Session.getUser().getEmail();
var email_ID1 = "email1@co.com";
var email_ID2 = "email2@co.com";
var email_ID3 = "email3@co.com";
var subject = "Change Notice Log";
var body = "Attached is the Change Notice Log. You will be asked if you trust this attachment, please select 'Yes' at the prompt. This daily report will capture all submittals, not just the day sent, so please refer to the timestamp column for the most recent date. This log is scheduled to automatically send on a daily basis, regardless of new submissions. If you have any questions, please contact HR Team. Thanks! ";
var requestData = {"method": "GET", "headers":{"Authorization":"Bearer "+ScriptApp.getOAuthToken()}};
var shID = getSheetID("Secondary") //Get Sheet ID of sheet name "Secondary"
var url = "https://docs.google.com/spreadsheets/d/"+ ssID + "/export?format=xlsx&id="+ssID+"&gid="+shID;
var result = UrlFetchApp.fetch(url , requestData);
var contents = result.getContent();
MailApp.sendEmail(email_ID1,subject ,body, {attachments:[{fileName:sheetName+".xls", content:contents, mimeType:"application//xls"}]});
MailApp.sendEmail(email_ID2,subject ,body, {attachments:[{fileName:sheetName+".xls", content:contents, mimeType:"application//xls"}]});
MailApp.sendEmail(email_ID3,subject ,body, {attachments:[{fileName:sheetName+".xls", content:contents, mimeType:"application//xls"}]});
};
function getSheetID(name){
var ss = SpreadsheetApp.getActive().getSheetByName(name)
var sheetID = ss.getSheetId().toString()
return sheetID
}
希望有所帮助。