Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.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
For loop 谷歌应用程序脚本,通过过滤器循环并发送带有PDF的电子邮件?_For Loop_Google Apps Script_Google Sheets_Filter - Fatal编程技术网

For loop 谷歌应用程序脚本,通过过滤器循环并发送带有PDF的电子邮件?

For loop 谷歌应用程序脚本,通过过滤器循环并发送带有PDF的电子邮件?,for-loop,google-apps-script,google-sheets,filter,For Loop,Google Apps Script,Google Sheets,Filter,我有来自20K行问卷的数据,我需要与门店经理分享我们400家店铺的报告。我设法编写了一个脚本,将我的工作表的pdf发送到电子邮件地址列表中。但是我一直在为过滤器编写循环,因为我无法让setVisibleValuesvalues函数为FilterCriteriaBuilder工作。setHiddenValuesvalues函数可以工作,但我不知道如何将其与循环结合起来 有关我的当前代码,请参见下文: /** * Filtersheet by location */ function Filte

我有来自20K行问卷的数据,我需要与门店经理分享我们400家店铺的报告。我设法编写了一个脚本,将我的工作表的pdf发送到电子邮件地址列表中。但是我一直在为过滤器编写循环,因为我无法让setVisibleValuesvalues函数为FilterCriteriaBuilder工作。setHiddenValuesvalues函数可以工作,但我不知道如何将其与循环结合起来

有关我的当前代码,请参见下文:

/**
 * Filtersheet by location
 */
function FilterSheet() {
  var spreadsheet = SpreadsheetApp.getActive().getSheetByName('Data')
  spreadsheet.getRange('F1').activate();
  var criteria = SpreadsheetApp.newFilterCriteria()
  .setHiddenValues(['Amsterdam, Rotterdam'])
  .build();
  spreadsheet.getFilter().setColumnFilterCriteria(6, criteria);
};

/**

 * Send pdf of currentspreadsheet
 */
function SendPdf() {
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var spreadsheet = SpreadsheetApp.getActive().getSheetByName('Adres');
  var blob = DriveApp.getFileById(ss.getId()).getAs("application/pdf");
  blob.setName(ss.getName() + ".pdf");

  var startRow = 2; // First row of data to process
  var numRows = 2; // Number of rows to process
  // Fetch the range of cells A2:B3
  var dataRange = spreadsheet.getRange(startRow, 1, numRows, 2);
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (var i in data) {
    var row = data[i];
    var emailAddress = row[0]; // First column
    var message = 'I hearby send you the overview of your data'
    var subject = 'Overview of data';
    MailApp.sendEmail(emailAddress, subject, message,{
      attachments:[blob]});
  }
 }
getValues返回所有区域单元格的值,无论它们是显示还是隐藏

使用循环和获取所有过滤值。可以使用Array.prototype.push将值添加到新数组中,也可以使用Array.prototype.splice在getValues返回的值中修改数组保持

相关的


我设法解决了这个问题

这个脚本使用一个包含两张表单的google电子表格,一张包含数据,另一张包含电子邮件地址组合。 它在sheet EmailAddresss var mode email中向相应的沙龙位置发送一个过滤列表过滤列F的工作表数据。此外,它还可以选择将pdf存储在您的google drive var模式存储中

*/

function construct() {
  // settings:
  //var mode = "store";
  var mode = "email";

  // get list of all salons and email
  var salonList = SpreadsheetApp.getActive().getSheetByName('EmailAdressen');
  // set endvar for loop
  var endRow = salonList.getLastRow();

  // loop trough the rows to get the Salon name and the corresponding email
  for(i=1;i<=endRow;i++){
    var salonName = salonList.getRange(i,2).getValue();
    var email = salonList.getRange(i,1).getValue();    

    // create an array with all salons that should be hidden (we cant pick which one to show, so we have to go the other way around...)
    var filterArray = [];
    // create array with all salons to hide
    for(c=1;c<=endRow;c++){
      // get value from email list, check if it is not the current selected one and if so add it to the list to filter out
      salonFilterName = salonList.getRange(c,2).getValue();
      if(salonFilterName != salonName) {
        filterArray.push(salonFilterName);
      }
    } // end for c

    // filter the list with the array we just created
    var spreadsheet = filterList(filterArray);

    if(mode == "email"){
      // export to PDF
      var pdf = exportToPdf(spreadsheet);
      // email to email address belonging to this salon
      emailToAddress(email, pdf);

    } // end if
    if(mode == "store"){
      StorePdf(spreadsheet, salonName);
    }

  } // end for i
  return;
}

function filterList(salonNameArray) {

  // select data sheet
  var spreadsheet = SpreadsheetApp.getActive().getSheetByName('Data');

  // first remove all existing filters to make sure we are on a clean sheet
  if(spreadsheet.getFilter()){
    spreadsheet.getFilter().remove(); 
  }

  // create the filter
  spreadsheet.getRange('F:F').createFilter();

  // set criteria for filter with array passed from construct
  var criteria = SpreadsheetApp.newFilterCriteria().setHiddenValues(salonNameArray).build();

  // apply filter
  spreadsheet.getFilter().setColumnFilterCriteria(6, criteria);
  return spreadsheet;
}

function exportToPdf(ss) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var spreadsheet = SpreadsheetApp.getActive().getSheetByName('Data');
  var blob = DriveApp.getFileById(ss.getId()).getAs("application/pdf");
  blob.setName(ss.getName() + ".pdf");

  return blob;
 }

function StorePdf(ss, salonName) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var spreadsheet = SpreadsheetApp.getActive().getSheetByName('Data');
  var blob = DriveApp.getFileById(ss.getId()).getBlob();
  blob.setName(salonName + "_" + Utilities.formatDate(new Date(), "GMT+1", "ddMMyyyy")+".pdf");

  DriveApp.createFile(blob);
  return;
 }

function emailToAddress(email, pdf) {
  MailApp.sendEmail(email, 'Type here the subject', 'Type here the body',{
      attachments:[pdf]});
  return;
}

分享您的数据表图像,并解释您想要做什么。因此,您的FilterSheet函数,但当您尝试将其导出为PDF sendPDF函数时,它不起作用或有不希望的行为,对吗?@Cooper感谢Cooper和Mateo的评论,我设法解决了问题。我在下面发布了我的解决方案。谢谢你的回复。你能再详细一点吗?我知道我可以使用isRowHiddenByFilterrowPosition和array.prototype.push创建一个新的隐藏值数组。但是如何设置过滤器以创建隐藏值并在所有过滤器选项中运行循环呢?我可以在pdf文件中捕获阵列的数据吗?