简体   繁体   English

setValues 不适用于 Google Apps 脚本中的数组

[英]setValues not working for arrays in Google Apps Script

I'm new to google apps script and I want to make a script for spreadsheet where I want to store the email addresses of others that has sent me, to my spreadsheet.我是google apps script新手,我想为电子表格制作一个脚本,我想将其他人发送给我的电子邮件地址存储到我的电子表格中。 I push all the emails to an array and use that array to store in the spreadsheet.push所有电子邮件push送到一个数组并使用该数组存储在电子表格中。 For some reason, .setValues() is not working though I see all the emails through log.出于某种原因,尽管我通过日志查看了所有电子邮件,但.setValues()不起作用。 Here are my codes,这是我的代码,

      var n=threads.length;
      var messages=thread.getMessages();
      var getfrom = 0;
      var allMails = [];
      for (var i=0; i<n; i++)
      {
         for (var j=0; j<messages.length; j++)
         {
            var message=messages[j];
            getfrom = message.getFrom();
            var first_name = getfrom.substring(0, getfrom.indexOf(" "));
            var last_name = getfrom.substring(getfrom.indexOf(" ")+1, getfrom.indexOf(" <"));
            var email_address = 0;
            if (first_name == '' && last_name == '')
            {
               email_address = getfrom;
            } else {
               email_address = getfrom.substring(getfrom.indexOf("<")+1, getfrom.indexOf(">"));
            }
          }
          allMails.push(email_address);
      }
      Logger.log(allMails);
      sheet1.getRange(2, 3, n, 1).setValues(allMails);
      Browser.msgBox("Operation complete");

How can I set all the values from the allMails[] array in my spreadsheet?如何在电子表格中设置allMails[]数组中的所有值? Need this help badly.非常需要这个帮助。 Thanks.谢谢。

setValues() needs a "two dimensional" array , as in an array of equal length arrays. setValues() 需要一个“二维”数组,就像在等长数组的数组中一样。

You want to supply the emails to the sheet as separate rows so try pushing the email addresses as single element arrays to the array, ie:您想将电子邮件作为单独的行提供给工作表,因此请尝试将电子邮件地址作为单个元素数组推送到数组,即:

allMails.push([email_address]);

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

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