Javascript 导出PDF时,Google应用程序脚本抛出#REF错误
我正在运行一个应用程序脚本,它可以完美地从电子表格中的有机表单导出PDF 问题是电子表格复制并在所有Javascript 导出PDF时,Google应用程序脚本抛出#REF错误,javascript,jquery,google-apps-script,google-sheets,Javascript,Jquery,Google Apps Script,Google Sheets,我正在运行一个应用程序脚本,它可以完美地从电子表格中的有机表单导出PDF 问题是电子表格复制并在所有单元格中抛出#REF error 这有什么办法吗?我确实看到了这一点,但我无法解决它。代码如下: 我假设你需要使用这个范围。复制到功能?但我想不出来 // Simple function to send Weekly Status Sheets to contacts listed on the "Contacts" sheet in the MPD. // Load a menu item c
单元格中抛出#REF error
这有什么办法吗?我确实看到了这一点,但我无法解决它。代码如下:
我假设你需要使用这个范围。复制到功能?但我想不出来
// Simple function to send Weekly Status Sheets to contacts listed on the "Contacts" sheet in the MPD.
// Load a menu item called "Project Admin" with a submenu item called "Send Status"
// Running this, sends the currently open sheet, as a PDF attachment
function onOpen() {
var submenu = [{name:"Send Status", functionName:"exportSomeSheets"}];
SpreadsheetApp.getActiveSpreadsheet().addMenu('Project Admin', submenu);
}
function exportSomeSheets() {
// Set the Active Spreadsheet so we don't forget
var originalSpreadsheet = SpreadsheetApp.getActive();
// Set the message to attach to the email.
var message = "Daily Sales Snapshot"; // Could make it a pop-up perhaps, but out of wine today
// Get Project Name from Cell A1
var projectname = originalSpreadsheet.getRange("A1:A1").getValues();
// Get Reporting Period from Cell B3
var period = originalSpreadsheet.getRange("B3:B3").getValues();
// Construct the Subject Line
var subject = projectname + " - Daily Status Sheet - " + period;
// Get contact details from "Contacts" sheet and construct To: Header
// Would be nice to include "Name" as well, to make contacts look prettier, one day.
var contacts = originalSpreadsheet.getSheetByName("Contacts");
var numRows = contacts.getLastRow();
var emailTo = contacts.getRange(2, 2, numRows, 1).getValues();
// Google scripts can't export just one Sheet from a Spreadsheet
// So we have this disgusting hack
// Create a new Spreadsheet and copy the current sheet into it.
var newSpreadsheet = SpreadsheetApp.create("Spreadsheet to export");
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var projectname = SpreadsheetApp.getActiveSpreadsheet();
sheet = originalSpreadsheet.getActiveSheet();
sheet.copyTo(newSpreadsheet);
//create temp sheet that lets us copy the values
sheet = originalSpreadsheet.getActiveSheet();
var temp = originalSpreadsheet.duplicateActiveSheet();
//copy the values
range = temp.getDataRange();
range.copyTo(range, {contentsOnly: true});
//copy the values to a new spreadsheet
temp.copyTo(newSpreadsheet);
//delete our temp sheet.
ss.deleteSheet(temp);
// Find and delete the default "Sheet 1", after the copy to avoid triggering an apocalypse
newSpreadsheet.getSheetByName('Sheet1').activate();
newSpreadsheet.deleteActiveSheet();
// Make zee PDF, currently called "Weekly status.pdf"
// When I'm smart, filename will include a date and project name
var pdf = DriveApp.getFileById(newSpreadsheet.getId()).getAs('application/pdf').getBytes();
var attach = {fileName:'Daily Status.pdf',content:pdf, mimeType:'application/pdf'};
// Send the freshly constructed email
MailApp.sendEmail(emailTo, subject, message, {attachments:[attach]});
// Delete the wasted sheet we created, so our Drive stays tidy.
DriveApp.getFileById(newSpreadsheet.getId()).setTrashed(true);
}
如果有不幸的灵魂出现在这一页上,这对我来说很有用
// Simple function to send Weekly Status Sheets to contacts listed on the "Contacts" sheet in the MPD.
// Load a menu item called "Project Admin" with a submenu item called "Send Status"
// Running this, sends the currently open sheet, as a PDF attachment
function onOpen() {
var submenu = [{name:"Send Status", functionName:"exportSomeSheets"}];
SpreadsheetApp.getActiveSpreadsheet().addMenu('Project Admin', submenu);
}
function exportSomeSheets() {
// Set the Active Spreadsheet so we don't forget
var originalSpreadsheet = SpreadsheetApp.getActive();
// Set the message to attach to the email.
var message = "Daily Sales Snapshot"; // Could make it a pop-up perhaps, but out of wine today
// Get Project Name from Cell A1
var projectname = originalSpreadsheet.getRange("H1:H1").getValues();
// Get Reporting Period from Cell B3
var period = originalSpreadsheet.getRange("B3:B3").getValues();
// Construct the Subject Line
var subject = projectname + " - Daily Status Sheet - " + period;
// Get contact details from "Contacts" sheet and construct To: Header
// Would be nice to include "Name" as well, to make contacts look prettier, one day.
var contacts = originalSpreadsheet.getSheetByName("Contacts");
var numRows = contacts.getLastRow();
var emailTo = contacts.getRange(2, 2, numRows, 1).getValues();
// Google scripts can't export just one Sheet from a Spreadsheet
// So we have this disgusting hack
var newSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheets = newSpreadsheet.getSheets;
for (var i = 5; i < sheets.length; i++) {
if (sheets[i].getSheetName() != sheetName) {
sheet[i].hideSheet();
}
}
// Make zee PDF, currently called "Weekly status.pdf"
// When I'm smart, filename will include a date and project name
var pdf = DriveApp.getFileById(newSpreadsheet.getId()).getAs('application/pdf').getBytes();
var attach = {fileName:'Daily Status.pdf',content:pdf, mimeType:'application/pdf'};
// Send the freshly constructed email
MailApp.sendEmail(emailTo, subject, message, {attachments:[attach]});
}
//向MPD中“联系人”表中列出的联系人发送每周状态表的简单功能。
//加载名为“项目管理”的菜单项和名为“发送状态”的子菜单项
//运行此操作,将当前打开的工作表作为PDF附件发送
函数onOpen(){
var子菜单=[{name:“发送状态”,functionName:“exportSomeSheets”}];
SpreadsheetApp.getActiveSpreadsheet().addMenu('Project Admin',子菜单);
}
函数exportSomeSheets(){
//设置活动电子表格,以便我们不会忘记
var originalSpreadsheet=SpreadsheetApp.getActive();
//将邮件设置为附加到电子邮件。
var message=“Daily Sales Snapshot”//可能会使它成为一个弹出窗口,但今天已经没有酒了
//从单元格A1获取项目名称
var projectname=originalSpreadsheet.getRange(“H1:H1”).getValues();
//从单元格B3获取报告周期
var period=originalSpreadsheet.getRange(“B3:B3”).getValues();
//构建主题线
var subject=projectname+“-每日状态表-”+期间;
//从“联系人”表中获取联系人详细信息并构造到:页眉
//如果有一天也能加上“姓名”,让联系人看起来更漂亮,那就太好了。
var contacts=originalSpreadsheet.getSheetByName(“contacts”);
var numRows=contacts.getLastRow();
var emailTo=contacts.getRange(2,2,numRows,1).getValues();
//Google脚本无法从电子表格中仅导出一张工作表
//所以我们有这个讨厌的黑客
var newSpreadsheet=SpreadsheetApp.getActiveSpreadsheet();
var sheets=newSpreadsheet.getSheets;
对于(变量i=5;i
我是否能够在脚本中以自动方式执行此操作?我期待着为这个脚本设置一个每日时间触发器,并通过电子邮件发送给一些人。在发布新问题之前,不要忘记做你的研究!谢谢@tehhowch!这完全有道理,多亏了其他人花时间回答!确保在所有“写入”命令之后调用SpreadsheetApp.flush()
,要求在下一条语句之前执行这些命令。(谷歌将对某些电话进行批处理以提高效率)