范围高度不正确 - Google Apps脚本

时间:2017-10-25 17:55:10

标签: javascript arrays google-apps-script

我目前正在尝试从其他电子表格中获取值,然后将其粘贴到目标电子表格中。我遇到的问题是,当我运行此代码时,我得到的范围高度不正确,范围宽度不正确。我读了一些关于2d数组的内容,但我相信我已经有了一个2d数组来粘贴到电子表格中。谢谢你的时间。

function GmailToDrive_StaticTest(gmailSubject, importFileID){


 var threads = GmailApp.search('subject:' + gmailSubject + ' -label:uploaded has:attachment'); // performs Gmail query for email threads

 for (var i in threads){
      var messages = threads[i].getMessages(); // finds all messages of threads returned by the query

      for (var j in messages){
           var attachments = messages[j].getAttachments(); // finds all attachments of found messages
           var timestamp = messages[j].getDate(); // receives timestamp of each found message
           var timestampMinusOne = new Date(timestamp.getTime() - (86400000)); // sets the received timestamp to exactly one day prior (# in milliseconds)
           var date = Utilities.formatDate(timestampMinusOne, "MST", "yyyy-MM-dd"); // cleans the timestamp string

           for (var k in attachments){
                var blobs = {
                      dataType: attachments[k].getContentType(), // retrives the file types of the attachments
                      data: attachments[k].copyBlob(), // creates blob files for every attachment
                      fileName: attachments[k].getName()
                      };

                var tempFile = DriveApp.createFile(blobs.data.setContentType('text/csv')).setName(blobs.fileName.split("-", 1).toString() + date); // creates csv files in drive's root per blob file

                var tempFileConverted = Drive.Files.copy( {}, tempFile.getId(), {convert: true} ); // converts created files to gsheets
                var importData = {
                      file: tempFileConverted,
                      ID: tempFileConverted.getId(),
                      Sheet1: SpreadsheetApp.openById(tempFileConverted.getId() ).getActiveSheet(),
                      Sheet1_Values: SpreadsheetApp.openById(tempFileConverted.getId() ).getActiveSheet().getDataRange().getValues()
                      };

                tempFile.setTrashed(true);

                var importData_Sheet1_Rows = importData.Sheet1.getMaxRows(); - 2;
                var importData_Sheet1_Columns = importData.Sheet1.getMaxColumns(); - 2;
                var destSheet = SpreadsheetApp.openById(importFileID).getSheets()[0]; 

                destSheet.clearContents();
                Logger.log(importData.Sheet1_Values)
                destSheet.getRange(1, 1, importData_Sheet1_Rows, importData_Sheet1_Columns).setValues(importData.Sheet1_Values);
                DriveApp.getFileById(importData.ID).setTrashed(true);    


           }
      }
 }

}

1 个答案:

答案 0 :(得分:2)

getMaxRows()getMaxColumns()会返回工作表中的最大列数和行数,而getDataRange().getValues()会返回包含数据的工作表中的所有值。

因此,除非工作表中的所有单元格都有数据,否则尺寸将不匹配!

您可以做的最好的事情是获取数据数组的实际大小,并使用它来设置目标表中值的范围。

它更像是这样:

destSheet.getRange(1, 1, importData.Sheet1_Values.length, importData.Sheet1_Values[0].length).setValues(importData.Sheet1_Values);

您不需要行和列的其他值,只需在脚本中忽略它。