Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. var targetFile = "12cyZSccaqnig-jBL0d1fyNxKT2bV-9LzXYUGIMtKjIQ"; // Output file
  2. var targetSheet = "Sheet1"; // output sheet
  3.  
  4.  
  5. function Main() {
  6. var searchedThreads = GmailApp.search( 'subject:"TEST"')[0];
  7. var id = searchedThreads.getId();
  8. var attachtextLength = GmailApp.getMessageById(id).getAttachments()[0].getDataAsString().length;
  9. if (attachtextLength <= 2000){
  10. var attachtext = GmailApp.getMessageById(id).getAttachments()[0].getDataAsString();
  11. UpdateSheet(attachtext);
  12. }
  13.  
  14. //GmailApp.sendEmail("", "Sent to Me", attachtext);
  15. }
  16.  
  17.  
  18. function UpdateSheet(dataString) {
  19. var dataArray = XLSToArray(dataString);
  20. var myFile = SpreadsheetApp.openById(targetFile);
  21. var mySheet = myFile.getSheetByName(targetSheet);
  22. var lastRowValue = mySheet.getLastRow();
  23. for (var i = 0; i < dataArray.length; i++) {
  24. mySheet.getRange(i+lastRowValue+1, 1, 1, dataArray[i].length).setValues(new Array(dataArray[i]));
  25. }
  26. mySheet.deleteRows(lastRowValue+1)
  27. }
  28.  
  29.  
  30.  
  31.  
  32. //Below is borrowed from https://developers.google.com/apps-script/articles/docslist_tutorial
  33. //The code formats the code so it can be entered into the Google Script
  34.  
  35.  
  36. function XLSToArray( strData, strDelimiter ){
  37. // Check to see if the delimiter is defined. If not,
  38. // then default to comma.
  39. strDelimiter = (strDelimiter || ",");
  40.  
  41.  
  42. // Create a regular expression to parse the XLS values.
  43. var objPattern = new RegExp(
  44. (
  45. // Delimiters.
  46. "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +
  47.  
  48.  
  49. // Quoted fields.
  50. "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
  51.  
  52.  
  53. // Standard fields.
  54. "([^\"\\" + strDelimiter + "\\r\\n]*))"
  55. ),
  56. "gi"
  57. );
  58.  
  59.  
  60.  
  61.  
  62. // Create an array to hold our data. Give the array
  63. // a default empty first row.
  64. var arrData = [[]];
  65.  
  66.  
  67. // Create an array to hold our individual pattern
  68. // matching groups.
  69. var arrMatches = null;
  70.  
  71.  
  72.  
  73.  
  74. // Keep looping over the regular expression matches
  75. // until we can no longer find a match.
  76. while (arrMatches = objPattern.exec( strData )){
  77.  
  78.  
  79. // Get the delimiter that was found.
  80. var strMatchedDelimiter = arrMatches[ 1 ];
  81.  
  82.  
  83. // Check to see if the given delimiter has a length
  84. // (is not the start of string) and if it matches
  85. // field delimiter. If id does not, then we know
  86. // that this delimiter is a row delimiter.
  87. if (
  88. strMatchedDelimiter.length &&
  89. (strMatchedDelimiter != strDelimiter)
  90. ){
  91.  
  92.  
  93. // Since we have reached a new row of data,
  94. // add an empty row to our data array.
  95. arrData.push( [] );
  96.  
  97.  
  98. }
  99.  
  100.  
  101.  
  102.  
  103. // Now that we have our delimiter out of the way,
  104. // let's check to see which kind of value we
  105. // captured (quoted or unquoted).
  106. if (arrMatches[ 2 ]){
  107.  
  108.  
  109. // We found a quoted value. When we capture
  110. // this value, unescape any double quotes.
  111. var strMatchedValue = arrMatches[ 2 ].replace(
  112. new RegExp( "\"\"", "g" ),
  113. "\""
  114. );
  115.  
  116.  
  117. } else {
  118.  
  119.  
  120. // We found a non-quoted value.
  121. var strMatchedValue = arrMatches[ 3 ];
  122.  
  123.  
  124. }
  125.  
  126.  
  127.  
  128.  
  129. // Now that we have our value string, let's add
  130. // it to the data array.
  131. arrData[ arrData.length - 1 ].push( strMatchedValue );
  132. }
  133.  
  134.  
  135. // Return the parsed data.
  136. return( arrData );
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement