Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 谷歌工作表将每张工作表转换为PDF格式,但不会';我不能继续格式化。我怎样才能做到这一点?_Google Apps Script - Fatal编程技术网

Google apps script 谷歌工作表将每张工作表转换为PDF格式,但不会';我不能继续格式化。我怎样才能做到这一点?

Google apps script 谷歌工作表将每张工作表转换为PDF格式,但不会';我不能继续格式化。我怎样才能做到这一点?,google-apps-script,Google Apps Script,我们正在尝试使用以下代码将单个工作表转换为单个PDF,并将PDF通过电子邮件发送到其中包含的电子邮件地址: function myFunction(){ var ss = SpreadsheetApp.openById("YOUR_SPREADSHEET_ID"); var sheets = ss.getSheets(); var B2Values = []; sheets.forEach(function(elt,index){ //Get a

我们正在尝试使用以下代码将单个工作表转换为单个PDF,并将PDF通过电子邮件发送到其中包含的电子邮件地址:

   function myFunction(){

    var ss = SpreadsheetApp.openById("YOUR_SPREADSHEET_ID");
    var sheets = ss.getSheets();

    var B2Values = [];
    sheets.forEach(function(elt,index){
    //Get all  your email adresses
    B2Values.push(sheets[index].getRange(2,2).getValue());
    });

    //For each sheet in your Spreadsheet, it create a temporary Spreadsheet
 //who got only one sheet, with your sheet values, 
// transform it as pdf, send the pdf by email to your B2 email adress and delete
//the temporary Spreadsheet

    sheets.forEach(function(elt, index){
    var temporarySS = SpreadsheetApp.create("NAME_OF_THE_FILE_WHO_WILL_BE_SENT");
    var temporaryId = temporarySS.getId();
    var dataToMove = sheets[index].getRange(1,1,sheets[index].getLastRow(),sheets[index].getLastColumn()).getValues();
    var openingTemporarySS = SpreadsheetApp.openById(temporaryId);
      dataToMove.forEach(function(elt){
      openingTemporarySS.appendRow(elt);
      })
     MailApp.sendEmail({
        to: B2Values[index],
        subject: "YOUR_SUBJECT",
        attachments: [openingTemporarySS.getAs('application/pdf')]
    })

    DriveApp.getFileById(temporaryId).setTrashed(true);

    });
    }
它工作得很好!但它不保留合并的单元格和常规格式。关于如何使这项工作成功,你有什么想法或想法吗?

  • 您希望将Google电子表格中的每张工作表发送到从单元格“B2”中检索为PDF文件的每封电子邮件
  • 您希望保留工作表的单元格格式
  • 您希望使用谷歌应用程序脚本实现这一点
如果我的理解是正确的,那么这个答案呢?请把这看作是几个可能的答案之一

修改点:
  • 在脚本中,将创建新的电子表格,并且仅将数据范围的值复制到其中。这样,就不会复制单元格格式
  • 为了复制值和格式,并将工作表创建为PDF数据,在您的情况下,我认为可以使用
    hideSheet()
    。例如,电子表格中有2张工作表,当第一个选项卡被
    hideSheet()
    隐藏并且电子表格作为PDF数据导出时,只有第二个选项卡作为PDF数据导出。我认为这可以用于你的情况。而且,这也可以降低工艺成本
当上述各点反映到脚本中时,它将变成如下所示

修改脚本: 参考资料:

如果我误解了你的问题,而这不是你想要的方向,我道歉。

@AndrewR谢谢你的回答。我很高兴你的问题解决了。也谢谢你。
function myFunction(){
  var ss = SpreadsheetApp.openById("YOUR_SPREADSHEET_ID");
  var sheets = ss.getSheets();
  var B2Values = [];
  sheets.forEach(function(elt,index){
    //Get all  your email adresses
    B2Values.push(sheets[index].getRange(2,2).getValue());  // You can also use B2Values.push(elt.getRange(2,2).getValue());
  });

  // I modified below script.
  B2Values.forEach(function(e, i) {
    sheets.forEach(function(sheet, j) {if (i != j) sheet.hideSheet()});
    // SpreadsheetApp.flush(); // By the situation, this line might be required.
    MailApp.sendEmail({
      to: e,
      subject: "YOUR_SUBJECT",
      attachments: [ss.getBlob()]
    });
    sheets.forEach(function(sheet) {sheet.showSheet()});
  });
}