简体   繁体   English

使用脚本将值和格式复制到Google工作表中的新工作表

[英]Copy Value and Format to new sheet in Google sheets using a script

In Google Apps Script, Im using the script below to create a pdf and send as an email. 在Google Apps脚本中,Im使用以下脚本创建pdf并作为电子邮件发送。 The problem is that when it creates the new sheet and copies across, the formulas are copied. 问题在于,当它创建新工作表并进行复制时,将复制公式。 The emailed PDF than shows a REF error. 通过电子邮件发送的PDF显示REF错误。

I need to copy the Value and Format. 我需要复制值和格式。

Thanks 谢谢

function exportPDF() {

var originalSpreadsheet = SpreadsheetApp.getActive();
var message = "Please see attached"; 
var projectname = originalSpreadsheet.getRange("a2:c2").getValues(); 
var period = originalSpreadsheet.getRange("B24:c24").getValues(); 
var subject = projectname + " - Daily Control Totals - " + period;
var contacts = originalSpreadsheet.getSheetByName("Contacts");
var numRows = contacts.getLastRow();
var emailTo = contacts.getRange(2, 2, numRows, 1).getValues();
var newSpreadsheet = SpreadsheetApp.create("Spreadsheet to export");
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var projectname = SpreadsheetApp.getActiveSpreadsheet();
sheet = originalSpreadsheet.getActiveSheet();
sheet.copyTo(newSpreadsheet);

newSpreadsheet.getSheetByName('Sheet1').activate();
newSpreadsheet.deleteActiveSheet();

DriveApp.getFileById(newSpreadsheet.getId()).getAs('application/pdf').getBytes();
var attach = {fileName:'Daily Totals.pdf',content:pdf, mimeType:'application/pdf'};

MailApp.sendEmail(emailTo, subject, message, {attachments:[attach]});
DriveApp.getFileById(newSpreadsheet.getId()).setTrashed(true);}

Currently, if you have references to other sheets in the sheet you are copying, after copying you will get REF errors, which makes sense as the data is no longer where it should be. 当前,如果您要复制的工作表中有其他工作表的引用,则在复制后会出现REF错误,这是有道理的,因为数据不再位于应有的位置。

To avoid that when we can use Range's copyTo(Range, Object); 为了避免这种情况,我们可以使用Range的 copyTo(Range, Object); method. 方法。 However, this method can only be used within a single spreadsheet, so we need to create a temporary sheet that will store our values then copy the sheet to a new spreadsheet to send in an email: 但是,此方法只能在单个电子表格中使用,因此我们需要创建一个临时表来存储我们的值,然后将其复制到新的电子表格中以通过电子邮件发送:

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = SpreadsheetApp.getActiveSheet();
  var newSS = SpreadsheetApp.create("Spreadsheet to export");

  var temp = ss.duplicateActiveSheet(); //create temp sheet that lets us copy the values
  range = temp.getDataRange();
  range.copyTo(range, {contentsOnly: true}); //copy the values

  temp.copyTo(newSS); //copy the values to a new spreadsheet
  ss.deleteSheet(temp); //delete our temp sheet.

Updated: So your code would look something like this: 更新:因此您的代码将如下所示:

function exportPDF() {

  var originalSpreadsheet = SpreadsheetApp.getActive();

  var message = "Please see attached"; 
  var projectname = originalSpreadsheet.getRange("a2:c2").getValues(); 
  var period = originalSpreadsheet.getRange("B24:c24").getValues(); 
  var subject = projectname + " - Daily Control Totals - " + period;
  var contacts = originalSpreadsheet.getSheetByName("Contacts");
  var numRows = contacts.getLastRow();
  var emailTo = contacts.getRange(2, 2, numRows, 1).getValues();

  var newSpreadsheet = SpreadsheetApp.create("Spreadsheet to export");
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

  sheet = originalSpreadsheet.getActiveSheet();
  var temp = originalSpreadsheet.duplicateActiveSheet(); //create temp sheet that lets us copy the values

  range = temp.getDataRange();
  range.copyTo(range, {contentsOnly: true}); //copy the values

  temp.copyTo(newSpreadsheet); //copy the values to a new spreadsheet
  ss.deleteSheet(temp); //delete our temp sheet.

  newSpreadsheet.getSheetByName('Sheet1').activate();
  newSpreadsheet.deleteActiveSheet();

  DriveApp.getFileById(newSpreadsheet.getId()).getAs('application/pdf').getBytes();
  var attach = {fileName:'Daily Totals.pdf',content:pdf, mimeType:'application/pdf'};

  MailApp.sendEmail(emailTo, subject, message, {attachments:[attach]});
  DriveApp.getFileById(newSpreadsheet.getId()).setTrashed(true);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将一张工作表的值复制到另一张工作表Google工作表脚本 - How to copy the value of one sheet to another sheet Google sheets Script 自动将表格复制到不带公式的新表格中(Google表格脚本) - Automatic copy table to a new Sheet without formula (Google Sheets script) 将基于单元格值的行数据复制到 Google 表格中的新工作表 - Copy row data based on cell value to new sheet in Google Sheets Google Sheets 脚本帮助请求:将选中的行从多个工作表复制到新工作表 - Google Sheets Script Help Request: Copy check boxed rows from multiple sheets to new sheet 用于将范围复制到另一张工作表的 Google 表格脚本 - Google Sheets script to copy a range to another sheet 将工作表中的值和格式复制到新的Google电子表格文档中? - Copy value and format from a sheet to a new google Spreadsheet document? 使用Google Apps脚本将行复制到新工作表 - Copy row to new sheet using Google Apps Script 从Google驱动器中可用的所有工作表中复制特定的单元格值,然后使用应用脚本粘贴到主工作表中 - Copy specific cell value from all sheets which are available in the google drive and paste in master sheet with apps script Google Sheets onEdit - 尝试通过更改同一行中列的值将一行复制到新工作表 - Google Sheets onEdit - trying to copy a row to a new sheet by changing the value of a column within that same row 谷歌表格 - 宏不会将图表的格式复制到另一个表格 - Google Sheets - Macro doesn´t copy the format of a Chart to another Sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM