0

I need to download a CSV file from an email. The system generates an email with a link to download the file. My final goal is to paste the data to a spreadsheet, but if I can get the file first, that'll be very helpful.

My function so far

  const threads = GmailApp.getUserLabelByName("labelName").getThreads();

  const messages = threads[0].getMessages();
  const body = messages[0].getBody();
  let html = "<div>" + body + "</div>";
  const doc = XmlService.parse(html).getRootElement()

This is the error

Exception: Error on line 2: The content of elements must consist of well-formed character data or markup.

HTML Version using .getBody()

<table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%"> 
           <tbody> 
            <tr> 
             <td align="left" vertical-align="middle" style="font-size:0px;padding:12px 0 8px 0;word-break:break-word;"> 
              <table border="0" cellpadding="0" cellspacing="0" role="presentation" style="border-collapse:separate;line-height:100%;"> 
               <tbody> 
                <tr> 
                 <td align="center" bgcolor="#0828cc" role="presentation"  valign="middle"> 
                   <a href="https://this-is-the-url.com/someids/someotherids/IAmAFile.zip"; target="_blank"> Download </a> 
                 </td> 
                </tr> 
               </tbody> 
              </table> </td> 
            </tr> 
           </tbody> 
          </table> 

Body Version by using getPlainBody() (In case it's easier to get the link from a plain text)

"
Hi, 
Please find attached your CSV report - *I Am A File Report* for date 
range: *08 September 2022 - 31 December 2022*. 
Download 
<https://this-is-the-url.com/someids/someotherids/IAmAFile.zip> 
View in the APP
<https://links-other-links.com> 
This report is scheduled for delivery every 1 hours . 
If you are facing any issue, please contact your admin. 
Regards, 
App Team 
"
Rubén
  • 34,714
  • 9
  • 70
  • 166
David Salomon
  • 804
  • 1
  • 7
  • 24
  • it was simple ```` function getLink() { const labelName = 'label' const threads = GmailApp.getUserLabelByName(labelName).getThreads(); const messages = threads[0].getMessages(); const body = messages[0].getPlainBody(); const rawLink = body.split("\n")[5]; const link = rawLink.replace("<", "").replace(">", ""); return link; } ``` – David Salomon Oct 01 '22 at 02:12

0 Answers0