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

喜你入骨 提交于 2020-01-16 14:04:28

问题


In Google Apps Script, Im using the script below to create a pdf and send as an email. 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.

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);}

回答1:


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.

To avoid that when we can use Range's 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);
}


来源:https://stackoverflow.com/questions/48992181/copy-value-and-format-to-new-sheet-in-google-sheets-using-a-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!