繁体   English   中英

通过Apps脚本检查Google表格的单元格内容,以确定是否应导出PDF

[英]Checking Google Sheets cell content via Apps Script to determine if a PDF should be exported

该脚本试图发送一封电子邮件,附件中包含一个已转换为PDF文件的表格的zip文件。 压缩文件中仅包含内容的工作表。

function zipit() {
  SpreadsheetApp.flush();

  var ss = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/[Google Key]/edit#");
// Base URL
  var url = "https://docs.google.com/spreadsheets/d/SS_ID/export?".replace("SS_ID", ss.getId());  
  var print1  = ss.getSheetByName("Print1");  
  var print2A = ss.getSheetByName("Print2A");
  var print2B = ss.getSheetByName("Print2B");
  var print2C = ss.getSheetByName("Print2C");
  var print3A = ss.getSheetByName("Print3A");

//get email address to send to 
  var organiserEmail = calcSheet.getRange("E31").getValue();   

//get date range for next month
  var tablesSheet = ss.getSheetByName("Next Month");
  var period = print1.getRange("V2:W2").getValue(); // period for the league taken from League 1
  var subject = "Singles League print outputs for : " + period;
  var body = "Attached are the PDF files to allow you to print the tables";

  var url_ext = 'exportFormat=pdf&format=pdf'        // export as pdf / csv / xls / xlsx
    + '&size=A4'                           // paper size legal / letter / A4
    + '&portrait=false'                    // orientation, false for landscape
    + '&fitw=true'                         // fit to page width, false for actual size
    + '&sheetnames=false&printtitle=false' // hide optional headers and footers
    //+ '&printRange'
    + '&pagenumbers=false'                 // hide page numbers
    + '&gridlines=true'                    // hide gridlines
    + '&fzr=false'                         // do not repeat row headers (frozen rows) on each page
    + '&gid=';                             // the sheet's Id

// Use token to access the sheet
  var options = {
    headers: {
      'Authorization': 'Bearer ' + ScriptApp.getOAuthToken(),
      'muteHttpExceptions': true,
    }
  };


// make empty array to hold fetched blobs
  var blobs = [];
  var i=0; // Set start number of 'blobs[]'

// **league 1**
  var SheetId = '56'; // id of League 1 print sheet - 'print1'
  var filename = '1' + '.pdf';
  // create PDF
    // Convert individual worksheets to PDF
    try { var response = UrlFetchApp.fetch(url + url_ext + SheetId, options); }
    catch(err) { Browser.msgBox("Error"); }

    // Convert the response to a blob and store in array
    blobs[i] = response.getBlob().setName(filename);
    i=i+1; // Set number of 'blob'

// **League 2A**
  // Check if sheet has content
  if (print2A.getRange("A11").getValue() != null) {
    SheetId = '58'; // id of League 1 print sheet - 'print2A'
  // create PDF
    // Convert individual worksheets to PDF
    try { var response = UrlFetchApp.fetch(url + url_ext + SheetId, options); }
    catch(err) { Browser.msgBox("Error"); }

    // Convert the response to a blob and store in array
    blobs[i] = response.getBlob().setName('2A' + '.pdf');
    i=i+1; // Set number of 'blob'
  }  

// **League 2B**
  // Check if sheet has content
  if (print2B.getRange("A11").getValue() != null) {
    SheetId = '59'; // id of League 2B print sheet - 'print2B'
  // create PDF
    // Convert individual worksheets to PDF
    try { var response = UrlFetchApp.fetch(url + url_ext + SheetId, options); }
    catch(err) { Browser.msgBox("Error"); }

    // Convert the response to a blob and store in array
    blobs[i] = response.getBlob().setName('2B' + '.pdf');
    i=i+1; // Set number of 'blob'
  }     

// **League 2C**
  //Check if sheet has content
  if (print2C.getRange("A11").getValue() != null) {
    SheetId = '62'; // id of League 2C print sheet - 'print2C'
  // create PDF
    // Convert individual worksheets to PDF
    try { var response = UrlFetchApp.fetch(url + url_ext + SheetId, options); }
    catch(err) { Browser.msgBox("Error"); }

    // Convert the response to a blob and store in array
    blobs[i] = response.getBlob().setName('2C' + '.pdf');
    var i=i+1; // Set number of 'blob'
  }

// **League 3A**
  // Check if sheet has content
  if (print3A.getRange("A11").getValue() != null) {
    SheetId = '60'; // id of League 3A print sheet - 'print3A'
  // create PDF
    // Convert individual worksheets to PDF
    try { var response = UrlFetchApp.fetch(url + url_ext + SheetId, options); }
    catch(err) { Browser.msgBox("Error"); }

    // Convert the response to a blob and store in array
    blobs[i] = response.getBlob().setName('3A' + '.pdf');
    i=i+1; // Set number of 'blob'
  }

/************************************************/
// New Participants
  SheetId = '38'; // id of New Participants print sheet - 'New Participants Alpha'

  // create PDF
  // Convert individual worksheets to PDF
  try { var response = UrlFetchApp.fetch(url + url_ext + SheetId, options); }
  catch(err) { Browser.msgBox("Error"); }

  // Convert the response to a blob and store in array
  blobs[i] = response.getBlob().setName('Participants' + '.pdf');

/************************************************/
// Send email
  // create a new blob that is a zip file containing our blob array
  var zipBlob = Utilities.zip(blobs).setName('Singles League :' + period + '.zip');

  // If allowed to send emails, send the email with the PDF attachment
  if (MailApp.getRemainingDailyQuota() > 0) 
     GmailApp.sendEmail(organiserEmail, subject, body, {
       htmlBody: body,
       attachments:[zipBlob]     
     }); 
}

想法是先打印第一张纸(联赛1),然后转到联赛2A,在该联赛中检查单元格A11(1,11),看是否有任何内容...如果该单元格中有内容,则转换页面到PDF并创建一个“ blob” ...如果A11中没有内容,则移至League 2B,测试单元格A11中的内容,如果没有内容,则移至League 2C,如果存在内容,则将页面转换为PDF并创建一个'blob'...以此类推,直到最后一个称为参与者的页面。 最后的任务是创建一个zipfile,并发送带有该zipfile的电子邮件。

问题是单元格“ A11”的测试不起作用。 所有工作表(1.pdf,2A.pdf,2B.pdf,2C.pdf,3A.pdf和Participants.pdf)都位于zip文件中,无论它们是否没有内容。 例程的电子邮件部分应正常工作。 参与者表不打印,而是打印了3A的副本...有人可以告知发生了什么并建议更正吗? 谢谢您的期待。

记录在工作代码下面以实现目标。...仅包括最后一部分:-

// Get token to access the sheet
   var token = ScriptApp.getOAuthToken();
   var options = {
        headers:{
        'Authorization': 'Bearer ' + token,
        'muteHttpExceptions': true,
        }

        };

  var SheetId = '56'; // id of League 1 print sheet - 'print1'
  var filename = '1' + '.pdf';
// create PDF
   // Convert individual worksheets to PDF
      try {
      var response = UrlFetchApp.fetch(url + url_ext + SheetId, options);
      }
      catch(err)
      {
      Browser.msgBox("Error");
      }

    // Convert the response to a blob and store in array
       blobs.push(response.getBlob().setName(filename));

// League 2A
  // Check if sheet has content
     if ((print2A.getRange("A11").getValue())!= ""){ 
     SheetId = '58'; // id of League 1 print sheet - 'print2A'
     filename = '2A' + '.pdf';
 // create PDF
    // Convert individual worksheets to PDF
       try {
       var response = UrlFetchApp.fetch(url + url_ext + SheetId, options);
       }
       catch(err)
       {
       Browser.msgBox("Error");
      }

     // Convert the response to a blob and store in array
        blobs.push(response.getBlob().setName(filename));

     }  

// League 2B
  // Check if sheet has content
     if ((print2B.getRange("A11").getValue())!= ""){
     SheetId = '59'; // id of League 2B print sheet - 'print2B'
     filename = '2B' + '.pdf';

// create PDF
   // Convert individual worksheets to PDF
      try {
      var response = UrlFetchApp.fetch(url + url_ext + SheetId, options);
          }
      catch(err)
      {
      Browser.msgBox("Error");
      }

    // Convert the response to a blob and store in array
       blobs.push(response.getBlob().setName(filename));

      }     

// League 2C
  //Check if sheet has content
    if ((print2C.getRange("A11").getValue())!= ""){
    SheetId = '62'; // id of League 2C print sheet - 'print2C'
    filename = '2C' + '.pdf';

 // create PDF
    // Convert individual worksheets to PDF
       try {
       var response = UrlFetchApp.fetch(url + url_ext + SheetId, options);
           }
       catch(err)
       {
       Browser.msgBox("Error");
       }

    // Convert the response to a blob and store in array
       blobs.push(response.getBlob().setName(filename));

       }

// League 3A
   // Check if sheet has content
      if ((print3A.getRange("A11").getValue())!= ""){
      SheetId = '60'; // id of League 3A print sheet - 'print3A'
      filename = '3A' + '.pdf';

// create PDF
    // Convert individual worksheets to PDF
       try {
       var response = UrlFetchApp.fetch(url + url_ext + SheetId, options);
           }
       catch(err)
          {
       Browser.msgBox("Error");
       }

    // Convert the response to a blob and store in array
       blobs.push(response.getBlob().setName(filename));

  }

// League 3B
   // Check if sheet has content
      if ((print3A.getRange("A11").getValue())!= ""){
      SheetId = '61'; // id of League 3B print sheet - 'print3B'
      filename = '3B' + '.pdf';

 // create PDF
    // Convert individual worksheets to PDF
       try {
        var response = UrlFetchApp.fetch(url + url_ext + SheetId, options);
           }
        catch(err)
         {
       Browser.msgBox("Error");
        }

    // Convert the response to a blob and store in array
       blobs.push(response.getBlob().setName(filename));

    }

  /************************************************/
 // New Participants
    SheetId = '38'; // id of New Participants print sheet - 'New Participants Alpha'
    filename = 'Participants' + '.pdf';
  // create PDF
    // Convert individual worksheets to PDF
       try {
       var response = UrlFetchApp.fetch(url + url_ext + SheetId, options);
           }
       catch(err)
       {
       Browser.msgBox("Error");
       }

    // Convert the response to a blob and store in array
       blobs.push(response.getBlob().setName(filename));

 /************************************************/
 // Send email
   // create a new blob that is a zip file containing our blob array
      var zipBlob = Utilities.zip(blobs).setName('Singles League :' + period + '.zip');

  // If allowed to send emails, send the email with the PDF attachment
    if (MailApp.getRemainingDailyQuota() > 0) {
        GmailApp.sendEmail(organiserEmail, subject, body, {
        htmlBody: body,
        attachments:[zipBlob]     
        });  
    };

}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM