<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-33774862</id><updated>2026-03-25T06:09:13.401-03:00</updated><category term="Lazarus"/><category term="automação"/><category term="ZeosLib"/><category term="IA"/><category term="finanças pessoais"/><category term="inteligência artificial"/><category term="SQLdb"/><category term="componentes lazarus"/><category term="python"/><category term="SQLite"/><category term="google sheets"/><category term="Firebird"/><category term="WinCE"/><category term="assistente financeiro pessoal"/><category term="lazreport"/><category term="DOM"/><category term="Data Module"/><category term="PostgreSQL"/><category term="Windows Mobile"/><category term="apps script"/><category term="automação financeira"/><category term="bot telegram"/><category term="fortesreport"/><category term="libreoffice"/><category term="mestre detalhe"/><category term="relatórios"/><category term="xml"/><category term="Diálogos"/><category term="Foms"/><category term="Free Pascal"/><category term="ImageList"/><category term="Linux"/><category term="MSAccess"/><category term="Menus"/><category term="MySQL"/><category term="ODBC"/><category term="Oracle"/><category term="PDA"/><category term="StatusBar"/><category term="TFixedFormatDataSet"/><category term="TList"/><category term="TSdfDataSet"/><category term="ToolBar"/><category term="VirtualBox"/><category term="aplicação console"/><category term="aplicação embarcada"/><category term="arquivos de projeto"/><category term="arquivos texto"/><category term="banco de dados"/><category term="configuração do lazarus"/><category term="data desktop"/><category term="dicionário de dados"/><category term="editor de código"/><category term="emulador WinCE"/><category term="estruturas de dados"/><category term="form dinâmico"/><category term="herança"/><category term="hint"/><category term="ide lazarus"/><category term="metas financeiras pessoais"/><category term="notificações"/><category term="object pascal"/><category term="produtividade"/><category term="progressbar"/><category term="relatório"/><category term="splash"/><category term="strings"/><category term="sysutils"/><category term="timer"/><category term="tipos procedurais"/><category term="transação"/><category term="virtualização"/><category term="xadrez"/><title type='text'>Professor Carlos - Finanças, Automação e Tecnologia</title><subtitle type='html'>Ajudo desenvolvedores e profissionais de tecnologia a organizar suas finanças e aumentar produtividade com automação e inteligência artificial.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://professorcarlos.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://professorcarlos.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Professor Carlos</name><uri>http://www.blogger.com/profile/10347668022241306511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFBZD8558AE67R-CINkytW3INbUINaAhDuZ7GKm_4DSSKeHSpdm3K-TPQrhNPn5B-21wN24ZH8flimqC7ikTzmRrYZBAsyD_JSC8YW8BDJZwmTby9R7LZC9SYkyHkSs7NPVZxXiqEDqjhkRJNxZwqqyZaVgYjISiFpzXsSyGZmL1YFCQ/s220/WhatsApp%20Image%202024-05-17%20at%2017.42.05.jpeg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>58</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-33774862.post-3629745557361973755</id><published>2026-02-27T08:59:00.000-03:00</published><updated>2026-02-27T08:59:14.104-03:00</updated><title type='text'>Relatório Financeiro Automático no E-mail com Apps Script e IA</title><content type='html'>&lt;h1 style=&quot;text-align: left;&quot;&gt;
  Relatório Financeiro Automático no E-mail com Apps Script e IA
&lt;/h1&gt;
&lt;p&gt;
  Nos posts anteriores,&amp;nbsp;&lt;a href=&quot;https://professorcarlos.blogspot.com/2026/02/como-usar-ia-para-planejar-metas-financeiras.html&quot; target=&quot;_blank&quot;&gt;Como usar IA para planejar metas financeiras realistas&lt;/a&gt;
  e&amp;nbsp;&lt;a href=&quot;https://professorcarlos.blogspot.com/2026/02/automatizando-o-planejamento-de-metas-com-ia-e-google-apps-script.html&quot; target=&quot;_blank&quot;&gt;Automatizando o Planejamento de Metas com IA e Google Apps Script&lt;/a&gt;, você aprendeu a usar IA para definir metas financeiras realistas e a
  conectar sua planilha à API da OpenAI para gerar análises automáticas. Hoje
  fechamos o ciclo: vamos fazer o sistema enviar um relatório financeiro
  completo por e-mail, com as análises da IA, direto para a sua caixa de entrada
  — todo dia 1º do mês, sem precisar abrir o Google Sheets.
&lt;/p&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center;&quot;&gt;
        &lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5bV_dkuRiYz23Y8yyJ1H9L_bjL5J6zDve-mWL2vc_07-yYCIWfSzu-AAV5CLuXakUXZdnOmcxEEbj25dpEX9IEKcyfShsDPFcwUMHU02qfJCanUV1qBvwy4lNmbst-17h5bf4xy6pk59LPQhWRpo1w4Ct1l3dxIqmbf7arNYxZxyaVtGfLKNY/s5000/17824.jpg&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;3001&quot; data-original-width=&quot;5000&quot; height=&quot;384&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5bV_dkuRiYz23Y8yyJ1H9L_bjL5J6zDve-mWL2vc_07-yYCIWfSzu-AAV5CLuXakUXZdnOmcxEEbj25dpEX9IEKcyfShsDPFcwUMHU02qfJCanUV1qBvwy4lNmbst-17h5bf4xy6pk59LPQhWRpo1w4Ct1l3dxIqmbf7arNYxZxyaVtGfLKNY/w640-h384/17824.jpg&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;
        Fonte:&amp;nbsp;https://br.freepik.com/
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;O que será implementado&lt;/h2&gt;
&lt;p&gt;
  Ao final deste tutorial, sua planilha irá apresentar três novas
  funcionalidades no menu 🤖 &lt;b&gt;Análise IA&lt;/b&gt;:
&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
  &lt;li&gt;&lt;b&gt;Enviar relatório por e-mail&lt;/b&gt; — disparo manual com um clique&lt;/li&gt;
&lt;/ul&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
  &lt;li&gt;
    &lt;b&gt;Ativar envio automático mensal&lt;/b&gt; — configura um trigger (gatilho) para
    rodar todo dia 1º do mês às 08h
  &lt;/li&gt;
&lt;/ul&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
  &lt;li&gt;
    &lt;b&gt;Desativar envio automático&lt;/b&gt; — remove o trigger quando quiser pausar
  &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;
  O e-mail gerado inclui um resumo financeiro do mês, as três análises da IA
  (resumo, alertas e sugestões) e um indicador visual de status (🟢 Saudável, 🟡
  Atenção ou 🔴 Crítico) baseado em regras que você mesmo define.
&lt;/p&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;Pré-requisitos&lt;/h2&gt;
&lt;p&gt;&lt;/p&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
  &lt;li&gt;O código dos posts anteriores já instalado no Apps Script da planilha&lt;/li&gt;
&lt;/ul&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
  &lt;li&gt;A chave da OpenAI já salva no &lt;i&gt;PropertiesService&lt;/i&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
  &lt;li&gt;
    A planilha com pelo menos a aba &lt;b&gt;IA_Analises&lt;/b&gt; preenchida (rode&amp;nbsp;&lt;i&gt;&lt;span style=&quot;font-style: normal;&quot;&gt;🤖&amp;nbsp;&lt;/span&gt;Análise IA&lt;b style=&quot;font-style: normal;&quot;&gt;
        →&amp;nbsp;&lt;/b&gt;Analisar minhas metas&lt;/i&gt;
    uma vez antes)
  &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;/p&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;O código Apps Script&lt;/h2&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
  Com o editor de script aberto, inclua as seguintes linhas no trecho de
  configuração que se encontra no início do código:
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;
&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
  &lt;pre style=&quot;line-height: 125%; margin: 0px; text-align: left;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; EMAIL_DESTINATARIO &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; Session.getActiveUser().getEmail(); &lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;// usa o e-mail do dono da planilha&lt;/span&gt;
&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; LIMITE_ALERTA_COMPROMETIMENTO &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;80&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;; &lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;// % da renda — acima disso dispara alerta crítico&lt;/span&gt;
&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; LIMITE_ALERTA_META &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;50&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;;            &lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;// % de cumprimento das metas — abaixo disso dispara alerta&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
  Em seguida, substitua a função &lt;i&gt;onOpen()&lt;/i&gt; por esta:
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;
&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
  &lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; onOpen() {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  SpreadsheetApp.getUi()&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    .createMenu(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;🤖 Análise IA&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    .addItem(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Analisar minhas metas&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;analisarMetas&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    .addSeparator()&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    .addItem(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Enviar relatório por e-mail&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;enviarRelatorioEmail&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    .addItem(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Ativar envio automático mensal&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;ativarTriggerMensal&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    .addItem(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Desativar envio automático&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;desativarTriggerMensal&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    .addToUi();&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
  Ela acrescenta mais três itens no menu&amp;nbsp;&lt;i&gt;&lt;span style=&quot;font-style: normal;&quot;&gt;🤖&amp;nbsp;&lt;/span&gt;Análise
    IA.&lt;b&gt;&amp;nbsp;&lt;/b&gt;&amp;nbsp;&lt;/i&gt;E, por fim, acrescente estas funções no final do script existente:
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;
&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
  &lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;//  ENVIO MANUAL DO RELATÓRIO&lt;/span&gt;
&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; enviarRelatorioEmail() {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; ss      &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; SpreadsheetApp.getActiveSpreadsheet();&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; analises &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; lerAbaComoObjeto(ss, &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;IA_Analises&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; resumo   &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; lerAbaComoObjeto(ss, &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Resumo_Mensal&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; config   &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; lerAbaComoObjeto(ss, &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Configurações&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;);&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;// Verifica se já existe análise gerada&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;analises[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Resumo mensal&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]) {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    SpreadsheetApp.getUi().alert(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Nenhuma análise encontrada. Execute &#39;Analisar minhas metas&#39; primeiro.&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  }&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; nivel  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; avaliarNivelAlerta(resumo, ss);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; html   &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; montarEmailHtml(config, analises, resumo, nivel);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; assunto &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; montarAssunto(config, nivel);&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  GmailApp.sendEmail(EMAIL_DESTINATARIO, assunto, &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;, { htmlBody&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; html });&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  SpreadsheetApp.getUi().alert(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;`✅ Relatório enviado para ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;EMAIL_DESTINATARIO&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}`&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: #6272a4;&quot;&gt;//  FUNÇÃO CHAMADA PELO TRIGGER AUTOMÁTICO&lt;/span&gt;
&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; enviarRelatorioAutomatico() {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; ss &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; SpreadsheetApp.getActiveSpreadsheet();&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;// Roda a análise de IA antes de enviar&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  analisarMetas();&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; analises &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; lerAbaComoObjeto(ss, &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;IA_Analises&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; resumo   &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; lerAbaComoObjeto(ss, &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Resumo_Mensal&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; config   &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; lerAbaComoObjeto(ss, &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Configurações&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;);&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; nivel  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; avaliarNivelAlerta(resumo, ss);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; html   &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; montarEmailHtml(config, analises, resumo, nivel);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; assunto &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; montarAssunto(config, nivel);&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  GmailApp.sendEmail(EMAIL_DESTINATARIO, assunto, &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;, { htmlBody&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; html });&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: #6272a4;&quot;&gt;//  AVALIAÇÃO DO NÍVEL DE ALERTA&lt;/span&gt;
&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; avaliarNivelAlerta(resumo, ss) {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; comprometido &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;parseFloat&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(resumo[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;% da Renda Comprometida&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]) &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; metas        &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; lerAbaComoTabela(ss, &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Metas_Mensais&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;);&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; metasComDesvio &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; metas.filter(m =&amp;gt; {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; ideal     &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;parseFloat&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(m[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Valor ideal&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]) &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; realizado &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;parseFloat&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(m[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Valor realizado&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]) &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; ideal &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; (realizado &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; ideal) &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; LIMITE_ALERTA_META;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  });&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; (comprometido &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; LIMITE_ALERTA_COMPROMETIMENTO &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; metasComDesvio.length &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;CRÍTICO 🔴&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; (comprometido &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;&amp;gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;65&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; metasComDesvio.length &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;===&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;ATENÇÃO 🟡&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;SAUDÁVEL 🟢&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: #6272a4;&quot;&gt;//  MONTAGEM DO ASSUNTO DO E-MAIL&lt;/span&gt;
&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; montarAssunto(config, nivel) {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; periodo &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; config[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Mês/Ano&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;este mês&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;`[${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;nivel&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}] Relatório de Metas Financeiras — ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;periodo&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}`&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: #6272a4;&quot;&gt;//  MONTAGEM DO HTML DO E-MAIL&lt;/span&gt;
&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; montarEmailHtml(config, analises, resumo, nivel) {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; corNivel &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; nivel.includes(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;CRÍTICO&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;?&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;#e74c3c&quot;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;                 &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; nivel.includes(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;ATENÇÃO&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;?&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;#f39c12&quot;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;                 &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;#27ae60&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;`&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;  &amp;lt;div style=&quot;font-family: Arial, sans-serif; max-width: 620px; margin: auto; color: #333;&quot;&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: #bd93f9;&quot;&gt;    &amp;lt;div style=&quot;background: ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;corNivel&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}; padding: 20px; border-radius: 8px 8px 0 0;&quot;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;      &amp;lt;h2 style=&quot;color: #fff; margin: 0;&quot;&amp;gt;📊 Relatório de Metas Financeiras&amp;lt;/h2&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;      &amp;lt;p style=&quot;color: #fff; margin: 6px 0 0;&quot;&amp;gt;${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;config[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Mês/Ano&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;&quot;} — Status: &amp;lt;strong&amp;gt;${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;nivel&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&amp;lt;/strong&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;    &amp;lt;/div&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: #bd93f9;&quot;&gt;    &amp;lt;div style=&quot;background: #f9f9f9; padding: 20px; border: 1px solid #ddd;&quot;&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: #bd93f9;&quot;&gt;      &amp;lt;h3 style=&quot;color: #555;&quot;&amp;gt;📌 Resumo do Mês&amp;lt;/h3&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;      &amp;lt;table style=&quot;width:100%; border-collapse: collapse; font-size: 14px;&quot;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;        &amp;lt;tr&amp;gt;&amp;lt;td style=&quot;padding:6px; border-bottom:1px solid #eee;&quot;&amp;gt;Total de Receitas&amp;lt;/td&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;            &amp;lt;td style=&quot;padding:6px; border-bottom:1px solid #eee; font-weight:bold;&quot;&amp;gt;R$ ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;resumo[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Total de Receitas&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;        &amp;lt;tr&amp;gt;&amp;lt;td style=&quot;padding:6px; border-bottom:1px solid #eee;&quot;&amp;gt;Total de Despesas&amp;lt;/td&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;            &amp;lt;td style=&quot;padding:6px; border-bottom:1px solid #eee; font-weight:bold;&quot;&amp;gt;R$ ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;resumo[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Total de Despesas&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;        &amp;lt;tr&amp;gt;&amp;lt;td style=&quot;padding:6px; border-bottom:1px solid #eee;&quot;&amp;gt;Saldo do Mês&amp;lt;/td&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;            &amp;lt;td style=&quot;padding:6px; border-bottom:1px solid #eee; font-weight:bold;&quot;&amp;gt;R$ ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;resumo[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Saldo do Mês&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;        &amp;lt;tr&amp;gt;&amp;lt;td style=&quot;padding:6px;&quot;&amp;gt;% da Renda Comprometida&amp;lt;/td&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;            &amp;lt;td style=&quot;padding:6px; font-weight:bold;&quot;&amp;gt;${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;resumo[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;% da Renda Comprometida&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;      &amp;lt;/table&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: #bd93f9;&quot;&gt;      &amp;lt;h3 style=&quot;color: #555; margin-top: 24px;&quot;&amp;gt;🤖 Análise da IA&amp;lt;/h3&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: #bd93f9;&quot;&gt;      &amp;lt;h4 style=&quot;margin-bottom: 4px;&quot;&amp;gt;Resumo mensal&amp;lt;/h4&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;      &amp;lt;p style=&quot;font-size: 14px; line-height: 1.6;&quot;&amp;gt;${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;analises[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Resumo mensal&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;—&quot;}&amp;lt;/p&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: #bd93f9;&quot;&gt;      &amp;lt;h4 style=&quot;margin-bottom: 4px; color: ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;corNivel&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;};&quot;&amp;gt;⚠️ Alertas&amp;lt;/h4&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;      &amp;lt;p style=&quot;font-size: 14px; line-height: 1.6;&quot;&amp;gt;${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;analises[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Alertas&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Nenhum alerta identificado.&quot;}&amp;lt;/p&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: #bd93f9;&quot;&gt;      &amp;lt;h4 style=&quot;margin-bottom: 4px; color: #2980b9;&quot;&amp;gt;💡 Sugestões&amp;lt;/h4&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;      &amp;lt;p style=&quot;font-size: 14px; line-height: 1.6;&quot;&amp;gt;${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;analises[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Sugestões&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;—&quot;}&amp;lt;/p&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: #bd93f9;&quot;&gt;    &amp;lt;/div&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: #bd93f9;&quot;&gt;    &amp;lt;div style=&quot;background: #eee; padding: 12px 20px; border-radius: 0 0 8px 8px; font-size: 12px; color: #888;&quot;&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;      Gerado automaticamente pela sua planilha de Metas Financeiras Inteligentes.&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;    &amp;lt;/div&amp;gt;&lt;/span&gt;

&lt;span style=&quot;color: #bd93f9;&quot;&gt;  &amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;  `&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: #6272a4;&quot;&gt;//  TRIGGER MENSAL — ATIVA (todo dia 1º às 08h)&lt;/span&gt;
&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; ativarTriggerMensal() {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;// Evita duplicação de triggers&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  desativarTriggerMensal();&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  ScriptApp.newTrigger(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;enviarRelatorioAutomatico&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    .timeBased()&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    .onMonthDay(&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    .atHour(&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    .create();&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  SpreadsheetApp.getUi().alert(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;✅ Envio automático ativado! Todo dia 1º às 08h você receberá o relatório.&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: #6272a4;&quot;&gt;//  TRIGGER MENSAL — DESATIVA&lt;/span&gt;
&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; desativarTriggerMensal() {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  ScriptApp.getProjectTriggers()&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    .filter(t =&amp;gt; t.getHandlerFunction() &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;===&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;enviarRelatorioAutomatico&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    .forEach(t =&amp;gt; ScriptApp.deleteTrigger(t));&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;// Só mostra alerta se chamado diretamente pelo menu&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; ui &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; SpreadsheetApp.getUi();&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;try&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; { ui.alert(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;🔕 Envio automático desativado.&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;); } &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;catch&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(e) {}&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;Como o nível de alerta é calculado&lt;/h2&gt;
&lt;div&gt;
  Antes de montar o e-mail, o código avalia dois critérios automaticamente:
&lt;/div&gt;
&lt;div&gt;
  &lt;ul style=&quot;text-align: left;&quot;&gt;
    &lt;li&gt;
      Se o &lt;b&gt;% da renda comprometida&lt;/b&gt; ultrapassa 80%, o status vai para 🔴
      &lt;b&gt;Crítico&lt;/b&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
  &lt;ul style=&quot;text-align: left;&quot;&gt;
    &lt;li&gt;
      Se &lt;b&gt;duas ou mais metas mensais&lt;/b&gt; estão abaixo de 50% de cumprimento,
      também vai para 🔴 &lt;b&gt;Crítico&lt;/b&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
  &lt;ul style=&quot;text-align: left;&quot;&gt;
    &lt;li&gt;Valores intermediários resultam em 🟡 &lt;b&gt;Atenção&lt;/b&gt;&lt;/li&gt;
  &lt;/ul&gt;
  &lt;ul style=&quot;text-align: left;&quot;&gt;
    &lt;li&gt;Tudo dentro do esperado resulta em 🟢 &lt;b&gt;Saudável&lt;/b&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&lt;div&gt;
  Você pode ajustar esses valores limites nas constantes
  &lt;i&gt;LIMITE_ALERTA_COMPROMETIMENTO&lt;/i&gt; e &lt;i&gt;LIMITE_ALERTA_META&lt;/i&gt; no topo do
  código.
&lt;/div&gt;
&lt;div&gt;
  &lt;h2 style=&quot;text-align: left;&quot;&gt;O e-mail em HTML&lt;/h2&gt;
  &lt;div&gt;
    O método &lt;i&gt;GmailApp.sendEmail()&lt;/i&gt; do Apps Script aceita o parâmetro
    &lt;i&gt;htmlBody&lt;/i&gt;, o que permite enviar e-mails formatados com tabelas, cores
    e tipografia — sem depender de nenhum serviço externo. A cor do cabeçalho
    muda dinamicamente conforme o nível de alerta: vermelho para crítico,
    amarelo para atenção e verde para saudável.
  &lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
  &lt;h2 style=&quot;text-align: left;&quot;&gt;Ativando o envio automático&lt;/h2&gt;
  &lt;div&gt;
    Depois de colar o código e salvar, volte para a planilha. No menu 🤖
    &lt;i&gt;Análise IA&lt;/i&gt;, clique em &lt;i&gt;Ativar envio automático mensal&lt;/i&gt;. O Apps
    Script vai registrar um trigger baseado em tempo que executa a função
    &lt;i&gt;enviarRelatorioAutomatico&lt;/i&gt; todo dia 1º do mês às 08h.
  &lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Os parâmetros dia e hora do envio do e-mail podem ser definidos na área de configuração do script.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
  &lt;b&gt;Importante&lt;/b&gt;: na primeira execução, o Google vai pedir autorização para
  que o script acesse seu Gmail. Isso é esperado — é o fluxo padrão de OAuth do
  Apps Script para escopos sensíveis.
&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
  Você pode verificar os triggers que estão ativos, a qualquer momento em
  &lt;i&gt;Apps Script → Acionadores&lt;/i&gt; (ícone de relógio na barra lateral).
&lt;/div&gt;
&lt;div&gt;
  &lt;h2 style=&quot;text-align: left;&quot;&gt;Resultado esperado&lt;/h2&gt;
  &lt;div&gt;No dia 1º de cada mês você recebe um e-mail com:&lt;/div&gt;
  &lt;div&gt;
    &lt;ul style=&quot;text-align: left;&quot;&gt;
      &lt;li&gt;
        O status do mês em destaque no assunto, por exemplo:
        &lt;i&gt;[SAUDÁVEL &lt;/i&gt;🟢&lt;i&gt;] Relatório de Metas Financeiras — Fevereiro/2026&lt;/i&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
    &lt;ul style=&quot;text-align: left;&quot;&gt;
      &lt;li&gt;
        Tabela com os números do mês (receitas, despesas, saldo, % comprometido)
      &lt;/li&gt;
    &lt;/ul&gt;
    &lt;ul style=&quot;text-align: left;&quot;&gt;
      &lt;li&gt;Os três blocos de análise da IA em linguagem natural&lt;/li&gt;
      &lt;li&gt;Rodapé identificando que foi gerado automaticamente pela planilha&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
  &lt;h2 style=&quot;text-align: left;&quot;&gt;Próximos passos possíveis&lt;/h2&gt;
  &lt;div&gt;
    Com esse sistema rodando, a evolução natural é trabalhar com histórico e
    tendências: salvar cada análise mensal na aba &lt;b&gt;Historico&lt;/b&gt; e, depois de
    alguns meses, pedir à IA que identifique padrões de comportamento financeiro
    ao longo do tempo — o que será tema de outro post no futuro.
  &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;i&gt;Ficou com alguma dúvida na configuração do trigger ou na autorização do Gmail? Deixa nos comentários!&lt;/i&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://professorcarlos.blogspot.com/feeds/3629745557361973755/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/33774862/3629745557361973755?isPopup=true' title='0 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/3629745557361973755'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/3629745557361973755'/><link rel='alternate' type='text/html' href='http://professorcarlos.blogspot.com/2026/02/relatorio-financeiro-automatico-no-e-mail-com-apps-script-e-ia.html' title='Relatório Financeiro Automático no E-mail com Apps Script e IA'/><author><name>Professor Carlos</name><uri>http://www.blogger.com/profile/10347668022241306511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFBZD8558AE67R-CINkytW3INbUINaAhDuZ7GKm_4DSSKeHSpdm3K-TPQrhNPn5B-21wN24ZH8flimqC7ikTzmRrYZBAsyD_JSC8YW8BDJZwmTby9R7LZC9SYkyHkSs7NPVZxXiqEDqjhkRJNxZwqqyZaVgYjISiFpzXsSyGZmL1YFCQ/s220/WhatsApp%20Image%202024-05-17%20at%2017.42.05.jpeg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5bV_dkuRiYz23Y8yyJ1H9L_bjL5J6zDve-mWL2vc_07-yYCIWfSzu-AAV5CLuXakUXZdnOmcxEEbj25dpEX9IEKcyfShsDPFcwUMHU02qfJCanUV1qBvwy4lNmbst-17h5bf4xy6pk59LPQhWRpo1w4Ct1l3dxIqmbf7arNYxZxyaVtGfLKNY/s72-w640-h384-c/17824.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-33774862.post-1333550113456606971</id><published>2026-02-22T08:54:00.004-03:00</published><updated>2026-02-27T08:55:51.729-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="apps script"/><category scheme="http://www.blogger.com/atom/ns#" term="assistente financeiro pessoal"/><category scheme="http://www.blogger.com/atom/ns#" term="automação financeira"/><category scheme="http://www.blogger.com/atom/ns#" term="IA"/><category scheme="http://www.blogger.com/atom/ns#" term="inteligência artificial"/><title type='text'>Automatizando o Planejamento de Metas com IA e Google Apps Script</title><content type='html'>&lt;h1 style=&quot;text-align: left;&quot;&gt;
  Automatizando o Planejamento de Metas com IA e Google Apps Script
&lt;/h1&gt;
&lt;p&gt;
  No
  &lt;a href=&quot;https://professorcarlos.blogspot.com/2026/02/como-usar-ia-para-planejar-metas-financeiras.html&quot; target=&quot;_blank&quot;&gt;post anterior&lt;/a&gt;, você aprendeu como a IA pode ajudar a definir metas financeiras realistas e
  recebeu uma planilha no Google Sheets para colocar esse planejamento em
  prática. Hoje vamos um passo além: vamos fazer a própria planilha chamar a IA
  automaticamente e escrever a análise para você — tudo com um clique, usando
  Google Apps Script e a API da OpenAI.
&lt;/p&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center;&quot;&gt;
        &lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgjJvaisBPP7TsX3vmpEgEf3ddUOtmmJ9DqrtD2aL4Bqi_8m6v1jpV65YbTOPgseW5A_YPJCMdgwjXE0fBBzPI72gFuqEYVeMniGxEtjFqCKlPft1ZdEuYrppZW_9G9Ih7cyPKOIHKwyEE0eMjWjpOGwely776z29mWwFSW7ypfoRTv2m0bfrjn/s4864/3d-graph-computer-illustration.jpg&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;3328&quot; data-original-width=&quot;4864&quot; height=&quot;438&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgjJvaisBPP7TsX3vmpEgEf3ddUOtmmJ9DqrtD2aL4Bqi_8m6v1jpV65YbTOPgseW5A_YPJCMdgwjXE0fBBzPI72gFuqEYVeMniGxEtjFqCKlPft1ZdEuYrppZW_9G9Ih7cyPKOIHKwyEE0eMjWjpOGwely776z29mWwFSW7ypfoRTv2m0bfrjn/w640-h438/3d-graph-computer-illustration.jpg&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;
        Fonte:&amp;nbsp;https://br.freepik.com/
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;O que vamos construir&lt;/h2&gt;
&lt;p&gt;
  Ao final deste tutorial, sua planilha terá um menu customizado 🤖
  &lt;b&gt;Análise IA&lt;/b&gt; que, ao ser acionado, vai:
&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
  &lt;li&gt;
    Ler seus dados financeiros do mês (receitas, despesas, objetivos e metas)
  &lt;/li&gt;
  &lt;li&gt;Montar um prompt contextualizado com essas informações&lt;/li&gt;
  &lt;li&gt;
    Enviar esse prompt para o modelo &lt;i&gt;gpt-4o&lt;/i&gt; (ou outro modelo de sua
    preferência) via API
  &lt;/li&gt;
  &lt;li&gt;
    Escrever os resultados diretamente na aba &lt;b&gt;IA_Analises&lt;/b&gt; da planilha,
    nas linhas de &lt;b&gt;Resumo mensal&lt;/b&gt;, &lt;b&gt;Alertas&lt;/b&gt; e &lt;b&gt;Sugestões&lt;/b&gt;
  &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;/p&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;Pré-requisitos&lt;/h2&gt;
&lt;p&gt;&lt;/p&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
  &lt;li&gt;
    A planilha do post anterior (se ainda não tem,
    &lt;a href=&quot;https://docs.google.com/spreadsheets/d/1bqgD9Lf7V9z0KFSedsABaJrM69buegtg0YiFoBhIVWs/copy&quot; target=&quot;_blank&quot;&gt;baixe aqui&lt;/a&gt;)
  &lt;/li&gt;
  &lt;li&gt;Uma conta na OpenAI Platform com créditos disponíveis&lt;/li&gt;
  &lt;li&gt;
    Conhecimento básico de JavaScript — o Apps Script usa uma variante dele
  &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;/p&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;Obtendo sua chave da API&lt;/b&gt;&lt;/h2&gt;
&lt;p&gt;
  Acesse
  &lt;a href=&quot;http://platform.openai.com/api-keys&quot; target=&quot;_blank&quot;&gt;platform.openai.com/api-keys&lt;/a&gt;, crie uma nova chave e guarde-a em um lugar seguro. Você vai usá-la em
  instantes.
&lt;/p&gt;
&lt;p&gt;
  &lt;a href=&quot;https://professorcarlos.blogspot.com/2025/11/seu-assistente-financeiro-pessoal-no-telegram-integrando-ia-ao-bot-do-google-sheets.html&quot; target=&quot;_blank&quot;&gt;Neste post&lt;/a&gt;
  mostramos em detalhes como obter sua chave da API OpenAI.
&lt;/p&gt;
&lt;p&gt;
  Atenção: nunca exponha sua API key em código compartilhado ou versionado. Mais
  abaixo mostramos como armazená-la de forma segura usando o
  &lt;i&gt;PropertiesService&lt;/i&gt; do próprio Apps Script.
&lt;/p&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;Acessando o Apps Script&lt;/h2&gt;
&lt;p&gt;
  Na sua planilha, vá em &lt;i&gt;Extensões → Apps Script&lt;/i&gt;. O editor será aberto em
  uma nova aba. Apague qualquer código padrão que estiver lá e cole o código
  abaixo.
&lt;/p&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;O Código Completo&lt;/h2&gt;
&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
  &lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;//  CONFIG — use PropertiesService em produção (veja abaixo)&lt;/span&gt;

&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; OPENAI_API_KEY &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; PropertiesService.getScriptProperties().getProperty(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;OPENAI_API_KEY&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;);&lt;/span&gt;
&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; OPENAI_MODEL   &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;gpt-4o&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: #6272a4;&quot;&gt;//  MENU CUSTOMIZADO&lt;/span&gt;

&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; onOpen() {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  SpreadsheetApp.getUi()&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    .createMenu(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;🤖 Análise IA&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    .addItem(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Analisar minhas metas&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;analisarMetas&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    .addToUi();&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: #6272a4;&quot;&gt;//  FUNÇÃO PRINCIPAL&lt;/span&gt;

&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; analisarMetas() {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; ss &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; SpreadsheetApp.getActiveSpreadsheet();&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; config    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; lerAbaComoObjeto(ss, &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Configurações&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; resumo    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; lerAbaComoObjeto(ss, &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Resumo_Mensal&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; objetivos &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; lerAbaComoTabela(ss, &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Objetivos&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; metas     &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; lerAbaComoTabela(ss, &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Metas_Mensais&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;);&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; prompt  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; montarPrompt(config, resumo, objetivos, metas);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; resposta &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; chamarOpenAI(prompt);&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;resposta) {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    SpreadsheetApp.getUi().alert(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Erro ao chamar a API da OpenAI. Verifique os logs.&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  }&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  escreverAnalises(ss, resposta);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  SpreadsheetApp.getUi().alert(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;✅ Análise concluída! Veja a aba IA_Analises.&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: #6272a4;&quot;&gt;//  HELPERS DE LEITURA&lt;/span&gt;

&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; lerAbaComoObjeto(ss, nomeAba) {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; aba  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; ss.getSheetByName(nomeAba);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;aba) &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; {};&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; dados &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; aba.getDataRange().getValues();&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; obj   &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; {};&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  dados.forEach(([campo, valor]) =&amp;gt; { &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; (campo) obj[campo] &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; valor; });&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; obj;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; lerAbaComoTabela(ss, nomeAba) {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; aba &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; ss.getSheetByName(nomeAba);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;aba) &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; [];&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; dados     &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; aba.getDataRange().getValues();&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; cabecalho &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; dados[&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;];&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; dados.slice(&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    .filter(linha =&amp;gt; linha.some(cel =&amp;gt; cel &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;!==&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; cel &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;!==&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;))&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    .map(linha =&amp;gt; {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;      &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; obj &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; {};&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;      cabecalho.forEach((col, i) =&amp;gt; { obj[col] &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; linha[i]; });&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;      &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; obj;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    });&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: #6272a4;&quot;&gt;//  MONTAGEM DO PROMPT&lt;/span&gt;

&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; montarPrompt(config, resumo, objetivos, metas) {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; objetivosTexto &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; objetivos.length&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;?&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; objetivos.map(o =&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;`- ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;o[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Objetivo&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;} | Tipo: ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;o[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Tipo&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;} | Prioridade: ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;o[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Prioridade&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;} | `&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;+&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;`Valor: R$ ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;o[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Valor total&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;} | Prazo: ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;o[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Prazo (meses)&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;} meses | Status: ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;o[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Status&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}`&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;      ).join(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;\n&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Nenhum objetivo cadastrado.&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; metasTexto &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; metas.length&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;?&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; metas.map(m =&amp;gt;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;`- ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;m[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Objetivo&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}: Ideal R$ ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;m[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Valor ideal&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;} | Mínimo R$ ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;m[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Valor mínimo&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;} | `&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;+&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;`Sugerido R$ ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;m[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Valor sugerido&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;} | Realizado R$ ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;m[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Valor realizado&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;} | `&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;+&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;`Diferença R$ ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;m[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Diferença&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}`&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;      ).join(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;\n&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Nenhuma meta mensal cadastrada.&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;`&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;Você é um consultor financeiro pessoal especializado em planejamento de metas.&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;Analise os dados financeiros abaixo e responda EXCLUSIVAMENTE no seguinte formato JSON:&lt;/span&gt;

&lt;span style=&quot;color: #bd93f9;&quot;&gt;{&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;  &quot;resumo_mensal&quot;: &quot;texto corrido com análise geral do mês&quot;,&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;  &quot;alertas&quot;: &quot;texto corrido com alertas sobre riscos ou desvios identificados&quot;,&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;  &quot;sugestoes&quot;: &quot;texto corrido com sugestões práticas e priorizadas&quot;&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: #bd93f9;&quot;&gt;=== DADOS DO USUÁRIO ===&lt;/span&gt;

&lt;span style=&quot;color: #bd93f9;&quot;&gt;Perfil: ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;config[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Perfil financeiro&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Não informado&quot;}&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;Período: ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;config[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Mês/Ano&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Não informado&quot;}&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;Renda total: R$ ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;config[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Renda total&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;Limite de comprometimento: ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;config[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Limite comprometimento (%)&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}%&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;Meta mínima de poupança: ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;config[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Meta mínima (%)&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}%&lt;/span&gt;

&lt;span style=&quot;color: #bd93f9;&quot;&gt;Resumo do mês:&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;- Total de Receitas: R$ ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;resumo[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Total de Receitas&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;- Total de Despesas: R$ ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;resumo[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Total de Despesas&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;- Saldo do Mês: R$ ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;resumo[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Saldo do Mês&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;- % da Renda Comprometida: ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;resumo[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;% da Renda Comprometida&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;- Capacidade para Metas: R$ ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;resumo[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Capacidade para Metas&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: #bd93f9;&quot;&gt;Objetivos cadastrados:&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;objetivosTexto&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: #bd93f9;&quot;&gt;Metas mensais:&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;metasTexto&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: #bd93f9;&quot;&gt;Responda apenas o JSON, sem markdown, sem explicações fora do JSON.&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;  `&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;.trim();&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: #6272a4;&quot;&gt;//  CHAMADA À API DA OPENAI&lt;/span&gt;

&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; chamarOpenAI(prompt) {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; url     &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;https://api.openai.com/v1/chat/completions&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; payload &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    model&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; OPENAI_MODEL,&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    messages&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; [{ role&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;user&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;, content&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; prompt }],&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    temperature&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0.7&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    response_format&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; { type&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;json_object&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; }&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  };&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; options &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    method&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;post&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    contentType&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;application/json&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    headers&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; { Authorization&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;`Bearer ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;OPENAI_API_KEY&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}`&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; },&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    payload&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;JSON&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;.stringify(payload),&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    muteHttpExceptions&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;true&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  };&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;try&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; response &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; UrlFetchApp.fetch(url, options);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; json     &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;JSON&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;.parse(response.getContentText());&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; (json.error) {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;      console.error(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Erro OpenAI:&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;, json.error.message);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;      &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    }&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;JSON&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;.parse(json.choices[&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;].message.content);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  } &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;catch&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; (e) {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    console.error(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Exceção:&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;, e.message);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  }&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: #6272a4;&quot;&gt;//  ESCRITA NA ABA IA_Analises&lt;/span&gt;

&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; escreverAnalises(ss, resposta) {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; aba   &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; ss.getSheetByName(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;IA_Analises&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;aba) &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; dados &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; aba.getDataRange().getValues();&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  dados.forEach((linha, i) =&amp;gt; {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; tipo &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; linha[&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;];&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; (tipo &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;===&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Resumo mensal&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)   aba.getRange(i &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;).setValue(resposta.resumo_mensal &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; (tipo &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;===&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Alertas&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)         aba.getRange(i &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;).setValue(resposta.alertas       &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; (tipo &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;===&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Sugestões&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)       aba.getRange(i &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;).setValue(resposta.sugestoes     &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  });&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;
  &lt;h2 style=&quot;text-align: left;&quot;&gt;Armazenando a chave com segurança&lt;/h2&gt;
  &lt;div&gt;
    Em vez de colocar a API key da OpenAI diretamente no código, use o
    &lt;i&gt;PropertiesService&lt;/i&gt;. Execute a função abaixo uma única vez pelo editor
    do Apps Script e depois a apague:
  &lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;
&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;
  &lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; salvarChave() {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  PropertiesService.getScriptProperties().setProperty(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;OPENAI_API_KEY&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;sk-SUA_CHAVE_AQUI&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;
  Se preferir pode usar o menu do projeto para armazenar a chave, assim como
  fizemos no post
  &lt;a href=&quot;https://professorcarlos.blogspot.com/2025/12/como-integrar-chatgpt-com-google-sheets-usando-google-apps-script.html&quot; target=&quot;_blank&quot;&gt;Como Integrar ChatGPT com Google Sheets usando Google Apps Script&lt;/a&gt;.
&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
  A partir daí, o código já busca a chave automaticamente com o código
  &lt;i&gt;PropertiesService.getScriptProperties().getProperty(&quot;OPENAI_API_KEY&quot;)&lt;/i&gt; —
  sem risco de exposição acidental.
&lt;/div&gt;
&lt;div&gt;
  &lt;h2 style=&quot;text-align: left;&quot;&gt;Entendendo o fluxo&lt;/h2&gt;
  &lt;div&gt;
    Vale a pena detalhar o que acontece por baixo dos panos quando o leitor
    clica no menu 🤖&amp;nbsp;&lt;b&gt;Análise IA&lt;/b&gt;:
  &lt;/div&gt;
  &lt;div&gt;&lt;br /&gt;&lt;/div&gt;
  &lt;div style=&quot;text-align: left;&quot;&gt;
    &lt;ol style=&quot;text-align: left;&quot;&gt;
      &lt;li&gt;
        &lt;b&gt;Leitura dos dados&lt;/b&gt; — as funções &lt;i&gt;lerAbaComoObjeto&lt;/i&gt; e
        &lt;i&gt;lerAbaComoTabela&lt;/i&gt; transformam os dados das abas em objetos
        JavaScript simples, filtrando linhas vazias automaticamente.
      &lt;/li&gt;
      &lt;li&gt;
        &lt;b&gt;Montagem do prompt&lt;/b&gt; — a função &lt;i&gt;montarPrompt&lt;/i&gt; injeta esses
        dados em um prompt estruturado que instrui o modelo a responder apenas
        em JSON, evitando texto livre que quebraria o parse, o processo de
        converter os dados recebidos.
      &lt;/li&gt;
      &lt;li&gt;
        &lt;b&gt;Chamada à API&lt;/b&gt; — &lt;i&gt;UrlFetchApp.fetch&lt;/i&gt; é o equivalente do fetch
        nativo no ambiente do Apps Script. O parâmetro
        &lt;i&gt;response_format: { type: &quot;json_object&quot; }&lt;/i&gt; garante que o
        &lt;i&gt;gpt-4o&lt;/i&gt; sempre retorne um JSON válido.
      &lt;/li&gt;
      &lt;li&gt;
        &lt;b&gt;Escrita dos resultados&lt;/b&gt; — a função
        &lt;i&gt;escreverAnalises&lt;/i&gt; percorre a aba &lt;b&gt;IA_Analises&lt;/b&gt; procurando as
        linhas pelo valor da coluna A (&quot;Resumo mensal&quot;, &quot;Alertas&quot;, &quot;Sugestões&quot;)
        e escreve o conteúdo correspondente na coluna B.
      &lt;/li&gt;
    &lt;/ol&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
  &lt;h2 style=&quot;text-align: left;&quot;&gt;Resultado esperado&lt;/h2&gt;
  &lt;div&gt;
    Após clicar em 🤖 &lt;b&gt;Análise IA → Analisar minhas metas&lt;/b&gt;, a aba
    &lt;b&gt;IA_Analises&lt;/b&gt; da planilha será preenchida automaticamente com três
    análises textuais geradas pelo &lt;i&gt;gpt-4o&lt;/i&gt; com base nos seus dados reais
    do mês.
  &lt;/div&gt;
  &lt;h2 style=&quot;text-align: left;&quot;&gt;Próximos passos possíveis&lt;/h2&gt;
  &lt;div&gt;Se quiser evoluir esse projeto, algumas ideias:&lt;/div&gt;
  &lt;div&gt;
    &lt;ul style=&quot;text-align: left;&quot;&gt;
      &lt;li&gt;
        &lt;b&gt;Histórico de análises&lt;/b&gt; — salvar cada análise na aba Historico com
        data e timestamp
      &lt;/li&gt;
    &lt;/ul&gt;
    &lt;ul style=&quot;text-align: left;&quot;&gt;
      &lt;li&gt;
        &lt;b&gt;Trigger automático&lt;/b&gt; — configurar um gatilho para a análise rodar
        todo dia 1º do mês sem intervenção manual
      &lt;/li&gt;
    &lt;/ul&gt;
    &lt;ul style=&quot;text-align: left;&quot;&gt;
      &lt;li&gt;
        &lt;b&gt;Análise do Simulador&lt;/b&gt; — incluir os dados da aba Simulador no
        prompt para que a IA avalie cenários de variação de renda e despesas
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/div&gt;
  &lt;div&gt;&lt;i&gt;
    Ficou com dúvida ou quer sugerir uma melhoria? Deixa nos comentários!
  &lt;/i&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://professorcarlos.blogspot.com/feeds/1333550113456606971/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/33774862/1333550113456606971?isPopup=true' title='0 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/1333550113456606971'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/1333550113456606971'/><link rel='alternate' type='text/html' href='http://professorcarlos.blogspot.com/2026/02/automatizando-o-planejamento-de-metas-com-ia-e-google-apps-script.html' title='Automatizando o Planejamento de Metas com IA e Google Apps Script'/><author><name>Professor Carlos</name><uri>http://www.blogger.com/profile/10347668022241306511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFBZD8558AE67R-CINkytW3INbUINaAhDuZ7GKm_4DSSKeHSpdm3K-TPQrhNPn5B-21wN24ZH8flimqC7ikTzmRrYZBAsyD_JSC8YW8BDJZwmTby9R7LZC9SYkyHkSs7NPVZxXiqEDqjhkRJNxZwqqyZaVgYjISiFpzXsSyGZmL1YFCQ/s220/WhatsApp%20Image%202024-05-17%20at%2017.42.05.jpeg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgjJvaisBPP7TsX3vmpEgEf3ddUOtmmJ9DqrtD2aL4Bqi_8m6v1jpV65YbTOPgseW5A_YPJCMdgwjXE0fBBzPI72gFuqEYVeMniGxEtjFqCKlPft1ZdEuYrppZW_9G9Ih7cyPKOIHKwyEE0eMjWjpOGwely776z29mWwFSW7ypfoRTv2m0bfrjn/s72-w640-h438-c/3d-graph-computer-illustration.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-33774862.post-2228971301867292608</id><published>2026-02-08T19:15:00.005-03:00</published><updated>2026-02-08T22:46:51.902-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="automação financeira"/><category scheme="http://www.blogger.com/atom/ns#" term="finanças pessoais"/><category scheme="http://www.blogger.com/atom/ns#" term="IA"/><category scheme="http://www.blogger.com/atom/ns#" term="inteligência artificial"/><category scheme="http://www.blogger.com/atom/ns#" term="metas financeiras pessoais"/><title type='text'>Como usar IA para planejar metas financeiras realistas</title><content type='html'>&lt;h1&gt;&lt;strong&gt;Como usar IA para planejar metas financeiras realistas&lt;/strong&gt;&lt;/h1&gt;
&lt;p&gt;Planejar metas financeiras é algo que quase todo mundo tenta fazer — e quase todo mundo abandona no meio do caminho.&lt;br /&gt;
Não por falta de vontade, mas por um motivo simples: &lt;strong&gt;as metas não são realistas, nem adaptadas à vida real&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Planilhas genéricas ignoram variáveis importantes, tais como: renda variável, imprevistos, comportamento humano e mudanças ao longo do tempo.&lt;br /&gt;
É aqui que a &lt;strong&gt;Inteligência Artificial&lt;/strong&gt; deixa de ser modismo e passa a ser ferramenta prática.&lt;/p&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgpze1BoB0oktYLsh57u7k6YAbUTRF_sZvv3sJDA2gc7ykxic7ciXmAF7s27LT7KGiIXOV5En6BDdHzPqm_hQDKRKV5O1fEMH4-kDKtRwQfTg8omkVZwscRZK4FIblz3CczcracnJRvEkWEAJxIE9BLzpoVJChLIC7jvaM8N2lG6crrZrX0lorL/s8268/saving-concept-ai-generated.jpg&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;3400&quot; data-original-width=&quot;8268&quot; height=&quot;264&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgpze1BoB0oktYLsh57u7k6YAbUTRF_sZvv3sJDA2gc7ykxic7ciXmAF7s27LT7KGiIXOV5En6BDdHzPqm_hQDKRKV5O1fEMH4-kDKtRwQfTg8omkVZwscRZK4FIblz3CczcracnJRvEkWEAJxIE9BLzpoVJChLIC7jvaM8N2lG6crrZrX0lorL/w640-h264/saving-concept-ai-generated.jpg&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Fonte:&amp;nbsp;https://br.freepik.com/&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;Neste artigo, você vai aprender:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;por que a maioria das metas financeiras falha,&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;como a IA pode ajudar a criar metas realistas,&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;e como usar uma &lt;strong&gt;planilha inteligente de metas&lt;/strong&gt;, pensada para se adaptar a você — e não o contrário.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;h2&gt;&lt;strong&gt;Por que metas financeiras falham tão frequentemente&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Antes de falar de IA, precisamos ser honestos.&lt;/p&gt;
&lt;p&gt;A maioria das metas financeiras falha porque:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;são copiadas de alguém que vive outra realidade,&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;ignoram gastos variáveis e imprevistos,&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;dependem exclusivamente de disciplina,&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;não são revisadas com dados reais.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Exemplos comuns de metas financeiras:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;“Vou guardar 30% do salário” (sem saber se sobra isso)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;“Vou quitar todas as dívidas em 6 meses” (sem considerar juros e fluxo de caixa)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;“Vou investir todo mês” (sem reserva de emergência)&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Metas assim não são ambiciosas — são &lt;strong&gt;mal calculadas&lt;/strong&gt;.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2&gt;&lt;strong&gt;Onde a IA entra nesse processo&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;A IA não serve para “adivinhar o futuro”, mas para &lt;strong&gt;analisar padrões, simular cenários e apoiar decisões melhores&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Quando aplicada às finanças pessoais, ela pode:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;analisar seu histórico de gastos,&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;identificar padrões de comportamento financeiro,&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;sugerir valores de metas baseados na sua realidade,&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;ajustar metas automaticamente quando algo muda.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Ou seja,&amp;nbsp;em vez de você se adaptar à planilha, &lt;strong&gt;a planilha se adapta a você&lt;/strong&gt;. A seguir eu mostro os passos para o planejamento de uma planilha.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2&gt;&lt;strong&gt;Passo 1: Organize seus dados financeiros&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Nenhuma IA funciona sem dados.&lt;/p&gt;
&lt;p&gt;Antes de qualquer meta, você precisa responder:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Quanto você ganha por mês (fixo e variável)?&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Quanto você gasta, de verdade?&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Quais gastos são fixos e quais variam?&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Quanto sobra (ou falta) no final do mês?&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Uma planilha bem estruturada deve conter:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;receitas,&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;despesas categorizadas,&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;saldo mensal,&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;histórico de pelo menos 3 meses.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;💡 &lt;strong&gt;Importante:&lt;/strong&gt; não busque perfeição. Busque consistência.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2&gt;&lt;strong&gt;Passo 2: Defina objetivos claros (não valores aleatórios)&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Uma meta financeira não começa com “quanto”, mas com &lt;strong&gt;“para quê”&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Exemplos de objetivos:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;criar uma reserva de emergência,&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;quitar dívidas específicas,&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;investir para médio ou longo prazo,&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;realizar um projeto pessoal.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A IA trabalha melhor quando o objetivo é claro, porque ela consegue:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;calcular prazos realistas,&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;sugerir aportes progressivos,&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;priorizar metas simultâneas.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;h2&gt;&lt;strong&gt;Passo 3: Use IA para transformar objetivos em metas possíveis&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Aqui está o ponto-chave.&lt;/p&gt;
&lt;p&gt;Em vez de definir metas “no chute”, você pode usar IA (como o ChatGPT integrado à planilha ou via prompt manual) para responder perguntas como:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Quanto posso guardar sem comprometer meu padrão de vida?&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Qual prazo é mais realista considerando meus gastos?&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;O que acontece se minha renda cair ou subir?&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Vale mais a pena quitar dívidas ou investir agora?&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A IA não decide por você — &lt;strong&gt;ela mostra cenários&lt;/strong&gt;. E isso muda tudo.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2&gt;&lt;strong&gt;Passo 4: Planeje metas flexíveis (e não engessadas)&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Metas rígidas quebram na primeira dificuldade.&lt;/p&gt;
&lt;p&gt;Metas inteligentes:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;têm valores mínimos e ideais,&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;se ajustam automaticamente ao saldo do mês,&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;permitem pausas sem “culpa financeira”.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Uma boa planilha com apoio de IA deve:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;recalcular metas se a renda mudar,&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;sugerir ajustes quando o saldo ficar negativo,&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;mostrar impacto de decisões antes de você tomá-las.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Isso reduz abandono e aumenta consistência.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2&gt;&lt;strong&gt;Passo 5: Acompanhe e revise com base em dados&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Planejar sem acompanhar é ilusão.&lt;/p&gt;
&lt;p&gt;A cada mês, você deve responder:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;A meta ainda faz sentido?&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;O valor planejado foi possível?&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;O comportamento financeiro mudou?&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Com IA, esse processo pode ser automatizado:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;resumo mensal automático,&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;alertas de desvio,&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;sugestões de ajuste de metas.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Assim, você sai do modo “culpa” e entra no modo &lt;strong&gt;gestão&lt;/strong&gt;.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2&gt;&lt;strong&gt;A planilha de metas financeiras inteligentes&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Para colocar tudo isso em prática, eu criei uma &lt;strong&gt;planilha de metas financeiras pensada para trabalhar com IA&lt;/strong&gt;, não contra você.&lt;/p&gt;
&lt;p&gt;Ela permite:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;definir objetivos claros,&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;simular cenários,&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;ajustar metas automaticamente,&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;acompanhar evolução mensal,&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;integrar com IA para análises e sugestões.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;📊 &lt;strong data-end=&quot;1643&quot; data-start=&quot;1563&quot;&gt;Clique no link abaixo para fazer uma cópia da planilha no seu Google Drive.&lt;/strong&gt;&lt;br data-end=&quot;1646&quot; data-start=&quot;1643&quot; /&gt;
A planilha é somente leitura. Ao clicar, você poderá salvá-la e adaptá-la à sua realidade financeira.&lt;/p&gt;&lt;p&gt;👉 &lt;strong&gt;&lt;a href=&quot;https://docs.google.com/spreadsheets/d/1bqgD9Lf7V9z0KFSedsABaJrM69buegtg0YiFoBhIVWs/copy&quot; target=&quot;_blank&quot;&gt;Baixar a planilha de metas financeiras inteligentes&lt;/a&gt;&lt;/strong&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Nos próximos artigos, vou mostrar:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;como integrar essa planilha com IA passo a passo,&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;como automatizar relatórios,&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;e como criar alertas inteligentes de metas.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr /&gt;
&lt;h2&gt;&lt;strong&gt;Conclusão&lt;/strong&gt;&lt;/h2&gt;
&lt;p&gt;Planejar metas financeiras não precisa ser doloroso, rígido ou frustrante.&lt;br /&gt;
Com dados, automação e IA, você transforma metas em processos — e processos em resultados.&lt;/p&gt;
&lt;p&gt;A tecnologia não substitui decisões.&lt;br /&gt;
Mas ajuda você a &lt;strong&gt;tomar decisões melhores, com menos estresse e mais clareza&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Se você quer que o dinheiro trabalhe a seu favor, comece pelo planejamento certo.&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://professorcarlos.blogspot.com/feeds/2228971301867292608/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/33774862/2228971301867292608?isPopup=true' title='0 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/2228971301867292608'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/2228971301867292608'/><link rel='alternate' type='text/html' href='http://professorcarlos.blogspot.com/2026/02/como-usar-ia-para-planejar-metas-financeiras.html' title='Como usar IA para planejar metas financeiras realistas'/><author><name>Professor Carlos</name><uri>http://www.blogger.com/profile/10347668022241306511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFBZD8558AE67R-CINkytW3INbUINaAhDuZ7GKm_4DSSKeHSpdm3K-TPQrhNPn5B-21wN24ZH8flimqC7ikTzmRrYZBAsyD_JSC8YW8BDJZwmTby9R7LZC9SYkyHkSs7NPVZxXiqEDqjhkRJNxZwqqyZaVgYjISiFpzXsSyGZmL1YFCQ/s220/WhatsApp%20Image%202024-05-17%20at%2017.42.05.jpeg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgpze1BoB0oktYLsh57u7k6YAbUTRF_sZvv3sJDA2gc7ykxic7ciXmAF7s27LT7KGiIXOV5En6BDdHzPqm_hQDKRKV5O1fEMH4-kDKtRwQfTg8omkVZwscRZK4FIblz3CczcracnJRvEkWEAJxIE9BLzpoVJChLIC7jvaM8N2lG6crrZrX0lorL/s72-w640-h264-c/saving-concept-ai-generated.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-33774862.post-1370513539877462469</id><published>2026-01-17T19:19:00.004-03:00</published><updated>2026-01-17T19:19:55.821-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="automação"/><category scheme="http://www.blogger.com/atom/ns#" term="finanças pessoais"/><category scheme="http://www.blogger.com/atom/ns#" term="libreoffice"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><title type='text'>Macros no LibreOffice: uma base conceitual</title><content type='html'>&lt;h1 style=&quot;text-align: left;&quot;&gt;Macros no LibreOffice: uma base conceitual&lt;/h1&gt;&lt;p&gt;Macros no LibreOffice são pequenos programas que permitem automatizar tarefas, estender funcionalidades e integrar a suíte com recursos externos. Elas podem ser usadas para desde ações simples — como preencher células, formatar planilhas ou criar botões — até soluções mais avançadas, como consumo de APIs, integração com bancos de dados e execução de scripts externos.&lt;/p&gt;&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuaPnM4OkGVEK1lq_R9eZZp2JaBUwVCMPA2BlBt_iEYYdBCz0Hpw3ou5-DaaoZH7IhF7P_Lrrsob7uVwwpXzhvpSiIyYB1NMT4mPGjGKdxB70bQbBEhV1nVEeoTvUyK0ng8hUwCsNFl8VRBrEBqorpcms-SlmnZCGSpVDy87LHcjtQ9ymE1LAN/s6092/closeup-hands-using-computer-laptop-with-screen-showing-analysis-data.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;4128&quot; data-original-width=&quot;6092&quot; height=&quot;434&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuaPnM4OkGVEK1lq_R9eZZp2JaBUwVCMPA2BlBt_iEYYdBCz0Hpw3ou5-DaaoZH7IhF7P_Lrrsob7uVwwpXzhvpSiIyYB1NMT4mPGjGKdxB70bQbBEhV1nVEeoTvUyK0ng8hUwCsNFl8VRBrEBqorpcms-SlmnZCGSpVDy87LHcjtQ9ymE1LAN/w640-h434/closeup-hands-using-computer-laptop-with-screen-showing-analysis-data.jpg&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Fonte:&amp;nbsp;https://br.freepik.com/&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;No contexto deste artigo, as macros são o elemento central que possibilita transformar o LibreOffice Calc em uma ferramenta poderosa para análise de dados financeiros e integração com inteligência artificial, como o ChatGPT.&lt;/p&gt;&lt;p&gt;O LibreOffice oferece suporte nativo a diferentes linguagens de macro, sendo as principais:&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • LibreOffice Basic (uma variação moderna do BASIC, fortemente integrada à API UNO) e&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Python (via ScriptForge, APSO e suporte nativo)&lt;/p&gt;&lt;p&gt;Este artigo apresenta uma visão geral da arquitetura de macros, conceitos fundamentais e boas práticas, preparando o terreno quem deseja entrar em detalhes mais específicos de Basic e Python.&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;A arquitetura de macros no LibreOffice&lt;/h2&gt;&lt;p&gt;O LibreOffice é construído sobre a UNO (Universal Network Objects), uma arquitetura de componentes que permite que diferentes linguagens interajam com os mesmos serviços internos da suíte.&lt;/p&gt;&lt;p&gt;Isso significa que, independentemente de você usar Basic ou Python, no final das contas estará acessando os mesmos objetos:&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Documentos (Calc, Writer, Impress, etc.),&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Planilhas e células,&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Estilos,&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Menus, barras de ferramentas e&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Serviços como diálogo, arquivos, rede, entre outros.&lt;/p&gt;&lt;p&gt;A diferença está na linguagem, na ergonomia do código e no ecossistema de bibliotecas disponíveis.&lt;/p&gt;&lt;p&gt;De forma simplificada:&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • O LibreOffice fornece a infraestrutura (UNO),&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • As macros são clientes dessa infraestrutura e&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Basic e Python são apenas duas formas diferentes de conversar com os mesmos serviços.&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Onde as macros ficam armazenadas&lt;/h2&gt;&lt;p&gt;As macros no LibreOffice podem existir em três níveis principais:&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Macros do usuário&lt;/h3&gt;&lt;p&gt;Ficam disponíveis para todos os documentos do usuário atual.&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • São ideais para funções reutilizáveis,&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Facilitam padronização e&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Podem ser versionadas externamente.&lt;/p&gt;&lt;p&gt;Normalmente ficam organizadas em:&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Minhas Macros&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ◦ LibreOffice Basic&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ◦ Python&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Macros do documento&lt;/h3&gt;&lt;p&gt;Ficam embutidas diretamente no arquivo (.ods, .odt, etc.).&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Viajam junto com o documento,&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Ideais para planilhas que serão distribuídas e&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Exigem atenção especial à segurança.&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Macros da aplicação&lt;/h3&gt;&lt;p&gt;Macros internas do LibreOffice.&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Segurança e nível de confiança&lt;/h2&gt;&lt;p&gt;Macros são código executável e, por isso, representam um risco potencial. O LibreOffice adota um modelo de segurança baseado em níveis de confiança:&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Macros desabilitadas,&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Macros habilitadas com confirmação e&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Macros sempre habilitadas (para locais confiáveis).&lt;/p&gt;&lt;p&gt;Para trabalhar com macros de forma produtiva, é altamente recomendável:&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Definir pastas confiáveis para seus projetos,&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Evitar habilitar macros indiscriminadamente e&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Separar ambientes de desenvolvimento e uso final.&lt;/p&gt;&lt;div style=&quot;text-align: left;&quot;&gt;A configuração padrão do LibreOffice é que macros estão desabilitadas.É necessário configurar de modo que permita a execução de macros. Então vá em&amp;nbsp;&lt;i&gt;Ferramentas -&amp;gt; Opções&lt;/i&gt;.&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;Clique em&amp;nbsp;&lt;i&gt;Segurança&lt;/i&gt;&amp;nbsp;e em&amp;nbsp;&lt;i&gt;Segurança de macros&lt;/i&gt;. Selecione a segurança&amp;nbsp;&lt;i&gt;Média&lt;/i&gt;, a qual exibe uma caixa de diálogo pede a confirmação do usuário antes da execução do script. Daí é só confirmar.&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtnVfvb4AdCQNVaBA5qREHZ4jLYmKABADbR_41Zp3KdMAQtcI67AywgyUl2i5to5cBVespmIgrox9A9zJ6OhfciicSIvDm97X6fUdomAHVXPiPdbf96vccTgsgO-y4cUML1_BBShes9QL3gPvqHrzoc5LARhoocPcLZKdRBMXi9svud9y0tUt0/s1535/Captura%20de%20tela%20de%202025-12-15%2018-15-51.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;830&quot; data-original-width=&quot;1535&quot; height=&quot;346&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtnVfvb4AdCQNVaBA5qREHZ4jLYmKABADbR_41Zp3KdMAQtcI67AywgyUl2i5to5cBVespmIgrox9A9zJ6OhfciicSIvDm97X6fUdomAHVXPiPdbf96vccTgsgO-y4cUML1_BBShes9QL3gPvqHrzoc5LARhoocPcLZKdRBMXi9svud9y0tUt0/w640-h346/Captura%20de%20tela%20de%202025-12-15%2018-15-51.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Tela de Configuração de Segurança de Macros&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;div&gt;O editor de macros&lt;/div&gt;&lt;p&gt;O LibreOffice fornece ferramentas diferentes dependendo da linguagem escolhida.&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Editor de LibreOffice Basic&lt;/h3&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Acessado via: Ferramentas → Macros → Organizar Macros → Basic,&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • IDE simples, porém integrada,&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Depurador básico disponível e&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Forte integração com diálogos e formulários.&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Editor de Python&lt;/h3&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Suporte nativo a scripts Python,&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Integração com a extensão APSO (Alternative Python Script Organizer),&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Possibilidade de usar editores externos (VS Code, PyCharm, etc.).&lt;/p&gt;&lt;p&gt;O uso de Python permite maior produtividade, melhor organização de código e acesso a bibliotecas modernas, algo especialmente relevante quando lidamos com dados financeiros e APIs externas.&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Organização de macros: pensando como um projeto&lt;/h2&gt;&lt;p&gt;Um erro comum é tratar macros como pequenos trechos isolados de código. É fundamental que adotemos uma abordagem diferente: macros como projetos de software.&lt;/p&gt;&lt;p&gt;Boas práticas incluem:&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Separar lógica de negócio da interface,&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Criar módulos reutilizáveis,&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Usar nomes claros e consistentes e&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Documentar funções e parâmetros.&lt;/p&gt;&lt;p&gt;Mesmo em Basic, é possível (e recomendável) organizar macros em módulos com responsabilidades bem definidas.&lt;/p&gt;&lt;p&gt;Essa organização é essencial quando criamos macros sobre dados financeiros e integração com GPT, onde a complexidade cresce rapidamente.&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Macros e automação financeira&lt;/h2&gt;&lt;p&gt;No contexto financeiro, macros no LibreOffice permitem:&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Importar cotações de ativos,&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Atualizar dados automaticamente,&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Calcular indicadores,&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Criar relatórios dinâmicos e&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Integrar com APIs externas.&lt;/p&gt;&lt;p&gt;No entanto, existem limitações importantes:&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • O ambiente de macro não é ideal para bibliotecas pesadas,&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Nem todas as dependências Python podem ser instaladas diretamente e&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Chamadas HTTP podem ser limitadas dependendo da linguagem.&lt;/p&gt;&lt;p&gt;Essas limitações explicam decisões arquiteturais, tais como o uso de scripts externos e comunicação entre processos.&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Basic vs Python: quando usar cada um&lt;/h2&gt;&lt;p&gt;Sem entrar ainda nos detalhes técnicos, algumas diretrizes gerais:&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;LibreOffice Basic:&lt;/h3&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Excelente integração nativa,&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Curva de aprendizado menor para automações simples e&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Ideal para interação direta com o usuário (botões, diálogos).&lt;/p&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Python:&lt;/h3&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Código mais expressivo e legível,&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Ecossistema vasto (APIs, dados, IA) e&lt;/p&gt;&lt;p&gt;&amp;nbsp; &amp;nbsp; • Melhor manutenção a médio e longo prazo.&lt;/p&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Considerações finais&lt;/h2&gt;&lt;p&gt;Este artigo teve como objetivo criar uma base conceitual sólida sobre macros no LibreOffice, sem ainda mergulhar em código específico.&lt;/p&gt;&lt;p&gt;Se desejar ir mais a fundo nos detalhes e desafios da criação mesmo de macros tanto em Basic quanto em Python veja nosso e-book &lt;a href=&quot;https://www.amazon.com.br/dp/B0GHFZ6QND&quot; target=&quot;_blank&quot;&gt;Dados Financeiros e ChatGPT com LibreOffice&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Ao longo do livro, você verá exemplos equivalentes em ambas as linguagens, permitindo comparar abordagens e tomar decisões técnicas mais conscientes.&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://professorcarlos.blogspot.com/feeds/1370513539877462469/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/33774862/1370513539877462469?isPopup=true' title='0 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/1370513539877462469'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/1370513539877462469'/><link rel='alternate' type='text/html' href='http://professorcarlos.blogspot.com/2026/01/macros-no-libreoffice-uma-base-conceitual.html' title='Macros no LibreOffice: uma base conceitual'/><author><name>Professor Carlos</name><uri>http://www.blogger.com/profile/10347668022241306511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFBZD8558AE67R-CINkytW3INbUINaAhDuZ7GKm_4DSSKeHSpdm3K-TPQrhNPn5B-21wN24ZH8flimqC7ikTzmRrYZBAsyD_JSC8YW8BDJZwmTby9R7LZC9SYkyHkSs7NPVZxXiqEDqjhkRJNxZwqqyZaVgYjISiFpzXsSyGZmL1YFCQ/s220/WhatsApp%20Image%202024-05-17%20at%2017.42.05.jpeg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuaPnM4OkGVEK1lq_R9eZZp2JaBUwVCMPA2BlBt_iEYYdBCz0Hpw3ou5-DaaoZH7IhF7P_Lrrsob7uVwwpXzhvpSiIyYB1NMT4mPGjGKdxB70bQbBEhV1nVEeoTvUyK0ng8hUwCsNFl8VRBrEBqorpcms-SlmnZCGSpVDy87LHcjtQ9ymE1LAN/s72-w640-h434-c/closeup-hands-using-computer-laptop-with-screen-showing-analysis-data.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-33774862.post-243029103762097387</id><published>2026-01-04T19:28:00.005-03:00</published><updated>2026-01-16T14:48:46.167-03:00</updated><title type='text'>Usando a biblioteca yfinance no LibreOffice Calc</title><content type='html'>&lt;h1 style=&quot;text-align: left;&quot;&gt;Usando a biblioteca &lt;i&gt;yfinance&lt;/i&gt; no LibreOffice Calc&lt;/h1&gt;&lt;div&gt;Já vimos no &lt;a href=&quot;https://professorcarlos.blogspot.com/2025/12/integrando-python-e-chatgpt-com-libreoffice-calc.html&quot; target=&quot;_blank&quot;&gt;post anterior&lt;/a&gt; que LibreOffice oferece recursos para rodar macros em Python. Um biblioteca de ferramentas denominada &lt;i&gt;ScriptForge&lt;/i&gt; busca simplificar o desenvolvimento dessas macros e a extensão &lt;i&gt;APSO (Alternative Python Script Organizer)&lt;/i&gt; facilita ainda mais a criação e organização.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;border: 1px solid rgb(0, 0, 0); text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdSi7UUhyj0DEEy-M5r6KskPUnoZELJ-RDBrJGj1YtKYm-q4Tee3vlJsfGFRjAsyA21rCeUgMNSnwTsdpPkUlLVEvAkqbbgjVMpyszhP6RrrRvK-sJ2vXPUtWC4tv8AEfM_Aj7gy8XCQa8IepovL5q9cdA-8okRmU3aw1jD2JeVWuYZ6eZEISU/s5000/40450.jpg&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;3750&quot; data-original-width=&quot;5000&quot; height=&quot;480&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdSi7UUhyj0DEEy-M5r6KskPUnoZELJ-RDBrJGj1YtKYm-q4Tee3vlJsfGFRjAsyA21rCeUgMNSnwTsdpPkUlLVEvAkqbbgjVMpyszhP6RrrRvK-sJ2vXPUtWC4tv8AEfM_Aj7gy8XCQa8IepovL5q9cdA-8okRmU3aw1jD2JeVWuYZ6eZEISU/w640-h480/40450.jpg&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Fonte:&amp;nbsp;https://br.freepik.com/&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;A interface de &lt;i&gt;APSO&lt;/i&gt; oferece atualmente uma versão beta do &lt;i&gt;Package manager&lt;/i&gt;, que se propõe a instalar e desinstalar pacotes &lt;i&gt;pip&lt;/i&gt;. Todavia, esse recurso não funciona para todas bibliotecas, entre elas a &lt;i&gt;yfinance&lt;/i&gt;. E é sobre isso que falaremos neste artigo, e como contornar essa dificuldade.&lt;/div&gt;&lt;br /&gt;&lt;div&gt;Antes de mais nada precisamos instalar a extensão &lt;i&gt;APSO&lt;/i&gt;. Veja no &lt;a href=&quot;https://professorcarlos.blogspot.com/2025/12/integrando-python-e-chatgpt-com-libreoffice-calc.html&quot; target=&quot;_blank&quot;&gt;post anterior&lt;/a&gt; como instalar e configurar o que precisamos na &lt;i&gt;APSO&lt;/i&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Depois de instalada a extensão, vamos analisar as dificuldades encontradas. A primeira delas é que as macros Python no LibreOffice executam em seu próprio ambiente virtual. Se você clicar em&amp;nbsp;&lt;i&gt;Ferramentas -&amp;gt; Macros -&amp;gt; Organizar scripts em Python,&lt;/i&gt;&amp;nbsp;em seguida no botão &lt;i&gt;Menu&lt;/i&gt; e &lt;i&gt;Python shell&lt;/i&gt;, verá que a versão do Python que executa é possivelmente diferente da versão que você tem no seu computador. Isso é um indício de que os ambientes são diferentes.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;O ambiente de execução de scripts dentro de softwares de planilha — como o LibreOffice — é limitado, controlado e isolado. Ele foi pensado para automações simples, integração com a própria planilha e pequenas rotinas de apoio. Não foi projetado para rodar bibliotecas modernas de mercado financeiro, que dependem de camadas mais profundas do sistema operacional, extensões nativas e dependências externas.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;Bibliotecas como o yfinance, por exemplo, utilizam componentes que exigem:&lt;/div&gt;&lt;div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;dependências compiladas&lt;/li&gt;&lt;li&gt;bibliotecas de baixo nível&lt;/li&gt;&lt;li&gt;controle preciso do ambiente Python&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;Forçar esse tipo de ferramenta a rodar dentro do ambiente interno da planilha gera uma cadeia de problemas: conflitos de versões, erros difíceis de diagnosticar, dependência do sistema do usuário e, principalmente, fragilidade.&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;A solução adotada neste artigo segue um princípio simples e poderoso: &lt;b&gt;cada ferramenta deve fazer apenas o que ela faz melhor&lt;/b&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;A planilha continua responsável por:&lt;/div&gt;&lt;div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;registrar dados&lt;/li&gt;&lt;li&gt;processar informações&lt;/li&gt;&lt;li&gt;apresentar visões claras&lt;/li&gt;&lt;li&gt;apoiar decisões&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;Já a coleta de dados externos — como cotações de mercado — acontece em um script Python independente, executado fora da planilha, em um ambiente controlado e previsível.&lt;/div&gt;&lt;div&gt;A comunicação entre esses dois mundos é feita de forma explícita, por meio da execução de um processo externo. A planilha pede a informação. O script busca, processa e devolve o resultado. Não há mágica, nem dependências invisíveis.&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;Essa separação, como se fossem duas camadas, traz vantagens importantes:&lt;/div&gt;&lt;div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;isolamento de erros&lt;/li&gt;&lt;li&gt;facilidade de manutenção&lt;/li&gt;&lt;li&gt;possibilidade de atualizar o script sem mexer na planilha&lt;/li&gt;&lt;li&gt;liberdade para usar bibliotecas modernas sem limitações&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;Resumindo, vamos escrever dois scripts. Um que executa em um ambiente Python no sistema do usuário. E a macro Python que executa no LibreOffice e chama o primeiro script por meio da biblioteca &lt;i&gt;subprocess&lt;/i&gt; do Python.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;A biblioteca &lt;i&gt;subprocess&lt;/i&gt; é usada para gerenciar e interagir com processos externos (programas ou comandos do sistema operacional) diretamente do seu script Python. Ou seja, a macro Python na planilha vai chamar um comando do shell do sistema operacional, tal como &lt;i&gt;Python script_externo.py&lt;/i&gt;, por exemplo.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Porém, antes de partirmos para a solução, precisamos falar das variáveis de ambiente &lt;i&gt;PYTHONHOME&lt;/i&gt; e &lt;i&gt;PYTHONPATH&lt;/i&gt;,&amp;nbsp;usadas pelo interpretador Python para localizar módulos e bibliotecas, mas servem a propósitos distintos e, na maioria das vezes, não precisam ser configuradas manualmente, e geralmente não são mesmo.&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Se você executar uma macro no LibreOffice que exibe essas variáveis, verá que &lt;i&gt;PYTHONPATH&lt;/i&gt; retorna caminhos das bibliotecas que a própria ferramenta utiliza para executar seus scripts, ou seja, o LibreOffice seta essas variáveis globalmente. Desse modo, essas variáveis vazam para o script externo quando o executamos via &lt;i&gt;subprocess&lt;/i&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Então vamos à nossa solução.&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Primeiro passo&lt;/h2&gt;&lt;div&gt;No shell do seu sistema, digite os seguintes comandos para criar uma pasta, um ambiente virtual e instalar as bibliotecas que serão usadas no script externo:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(248, 248, 248); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;mkdir&lt;span style=&quot;color: #bbbbbb;&quot;&gt; &lt;/span&gt;external_scripts
&lt;span style=&quot;color: green;&quot;&gt;cd&lt;/span&gt;&lt;span style=&quot;color: #bbbbbb;&quot;&gt; &lt;/span&gt;external_scripts
python3&lt;span style=&quot;color: #bbbbbb;&quot;&gt; &lt;/span&gt;-m&lt;span style=&quot;color: #bbbbbb;&quot;&gt; &lt;/span&gt;venv&lt;span style=&quot;color: #bbbbbb;&quot;&gt; &lt;/span&gt;.venv
&lt;span style=&quot;color: green;&quot;&gt;source&lt;/span&gt;&lt;span style=&quot;color: #bbbbbb;&quot;&gt; &lt;/span&gt;.venv/bin/activate
pip&lt;span style=&quot;color: #bbbbbb;&quot;&gt; &lt;/span&gt;install&lt;span style=&quot;color: #bbbbbb;&quot;&gt; &lt;/span&gt;yfinance&lt;/pre&gt;&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Segundo passo&lt;/h2&gt;&lt;div&gt;Crie o script externo denominado &lt;i&gt;price.py&lt;/i&gt;, por exemplo. Neste exemplo, o script espera um argumento, o qual deve ser um &lt;i&gt;ticker&lt;/i&gt; (código da empresa na bolsa) válido e usa o &lt;i&gt;yfinance&lt;/i&gt; para buscar o último preço desse ativo.&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; text-align: left; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; sys&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; yfinance &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; yf&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;ticker&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;sys&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;argv[&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;price&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;yf&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Ticker(ticker)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;history(period&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;1d&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Close&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;iloc[&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;
&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(price)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;Terceiro passo&lt;/h2&gt;&lt;div&gt;Crie a macro Python no LibreOffice Calc.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; subprocess&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; os&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;update_price&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;():&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;env&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;os&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;environ&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;copy()&lt;/span&gt;

    &lt;span style=&quot;color: #6272a4;&quot;&gt;# REMOVE as variáveis de ambiente do LibreOffice&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;env&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;pop(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;PYTHONHOME&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;env&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;pop(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;PYTHONPATH&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    
    &lt;span style=&quot;color: #6272a4;&quot;&gt;# Define o comando &quot;python price.py PETR3.SA&quot;&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;cmd&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;[&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;/home/carlos/external_scripts/.venv/bin/python&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;/home/carlos/external_scripts/price.py&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;PETR3.SA&quot;&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;
    
    &lt;span style=&quot;color: #6272a4;&quot;&gt;# Executa o comando no shell e captura o resultado (python price.py PETR3.SA)&lt;/span&gt;
    &lt;span style=&quot;color: #6272a4;&quot;&gt;# Aqui é necessário tratar exceções&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;result&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;subprocess&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;run(cmd,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;capture_output&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=True&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=True&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;env&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;env)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;price&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(result&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;stdout&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;strip())&lt;/span&gt;

    &lt;span style=&quot;color: #6272a4;&quot;&gt;# Escreve o preço na célula A1&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;doc&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;XSCRIPTCONTEXT&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;getDocument()&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;sheet&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;doc&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;CurrentController&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;ActiveSheet&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;sheet&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;getCellByPosition(&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Value&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;price&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Observe que removemos as variáveis de ambiente antes de executar o script externo. No &lt;i&gt;list cmd&lt;/i&gt; informamos exatamente o caminho do comando &lt;i&gt;python&lt;/i&gt;, o caminho do script &lt;i&gt;price.py&lt;/i&gt; e o argumento.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;O &lt;i&gt;subprocess&lt;/i&gt; retorna um objeto, do qual nos interessa &lt;i&gt;stdout&lt;/i&gt;, que é o que foi exibido na saída padrão. Esse valor é uma string, que é convertido para &lt;i&gt;float&lt;/i&gt; e exibido na célula A1. A exibição usa os recursos de macros da LibreOffice, tais como &lt;i&gt;XSCRIPTCONTEXT&lt;/i&gt;, uma interface&amp;nbsp;fornecida aos scripts que oferece um meio de acesso às diversas interfaces que eles podem precisar para executar alguma ação em um documento.&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Conclusão&lt;/h2&gt;&lt;div&gt;Esta é uma solução que roda exclusivamente na máquina local do usuário. Uma outra opção, que depende de outros recursos, é criar uma API REST que tem um endpoint para acesso à &lt;i&gt;yfinance&lt;/i&gt;. E a macro usaria &lt;i&gt;requests&lt;/i&gt; para fazer uma requisição a essa API.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Se você tem interesse em outras soluções de macros em Basic ou Python para LibreOffice dê uma olhada em&amp;nbsp;&lt;a href=&quot;https://stonefull.gumroad.com/l/financial_data_chatgpt_libreoffice&quot;&gt;https://stonefull.gumroad.com/l/financial_data_chatgpt_libreoffice&lt;/a&gt;.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://professorcarlos.blogspot.com/feeds/243029103762097387/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/33774862/243029103762097387?isPopup=true' title='0 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/243029103762097387'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/243029103762097387'/><link rel='alternate' type='text/html' href='http://professorcarlos.blogspot.com/2025/12/usando-biblioteca-yfinance-no-libreoffice-calc.html' title='Usando a biblioteca yfinance no LibreOffice Calc'/><author><name>Professor Carlos</name><uri>http://www.blogger.com/profile/10347668022241306511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFBZD8558AE67R-CINkytW3INbUINaAhDuZ7GKm_4DSSKeHSpdm3K-TPQrhNPn5B-21wN24ZH8flimqC7ikTzmRrYZBAsyD_JSC8YW8BDJZwmTby9R7LZC9SYkyHkSs7NPVZxXiqEDqjhkRJNxZwqqyZaVgYjISiFpzXsSyGZmL1YFCQ/s220/WhatsApp%20Image%202024-05-17%20at%2017.42.05.jpeg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgdSi7UUhyj0DEEy-M5r6KskPUnoZELJ-RDBrJGj1YtKYm-q4Tee3vlJsfGFRjAsyA21rCeUgMNSnwTsdpPkUlLVEvAkqbbgjVMpyszhP6RrrRvK-sJ2vXPUtWC4tv8AEfM_Aj7gy8XCQa8IepovL5q9cdA-8okRmU3aw1jD2JeVWuYZ6eZEISU/s72-w640-h480-c/40450.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-33774862.post-5561293583107148636</id><published>2025-12-16T18:51:00.009-03:00</published><updated>2026-01-16T14:50:03.214-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="automação"/><category scheme="http://www.blogger.com/atom/ns#" term="IA"/><category scheme="http://www.blogger.com/atom/ns#" term="inteligência artificial"/><category scheme="http://www.blogger.com/atom/ns#" term="libreoffice"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><title type='text'>Integrando Python e ChatGPT com LibreOffice Calc</title><content type='html'>&lt;h1 style=&quot;text-align: left;&quot;&gt;Integrando Python e ChatGPT com LibreOffice Calc&lt;/h1&gt;&lt;div&gt;O Excel agora permite executar scripts Python dentro de uma célula, simplesmente usando a fórmula &lt;i&gt;=PY&lt;/i&gt;. No entanto, essa é uma funcionalidade disponível apenas nas licenças pagas. O LibreOffice é um pacote para escritórios, a exemplo do Office da Microsoft, todavia livre e de código aberto. Ele tem planilha, editor de textos, editor de apresentação e muitas outras aplicações. E ainda roda em vários sistemas operacionais, incluindo Linux.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Se você prefere usar ferramentas gratuitas e ainda assim não abrir mão de recursos tais como rodar scripts Python numa célula de planilha, este artigo pode lhe interessar.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Neste post iremos apresentar a &lt;i&gt;LibrePythonista&lt;/i&gt;, uma extensão para LibreOffice que nos habilita usar Python dentro de uma planilha Calc, possibilitando que utlizemos a biblioteca &lt;i&gt;pandas&lt;/i&gt;, por exemplo, e assim, analisar dados usando um &lt;i&gt;DataFrame&lt;/i&gt;. Dado que podemos instalar qualquer biblioteca com essa extensão, iremos instalar e usar a &lt;i&gt;openai&lt;/i&gt;, para acessar o ChatGPT e criar conteúdo usando IA.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div class=&quot;separator&quot; style=&quot;border: 1px solid rgb(0, 0, 0); clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjo1aSyEUQe63CHcsX5YLhodaWUN2fKwusORdSW9rW9Xi5vJwSTYG3Z37wFr80ZEhGJg01K8hx5pKATdgFROmfaMm_oVqpZvwcc3YWHLwrRgNDAnB9_PyJPgONPGXuu77jyOCLQAfpPoVKoWQBsNlAQbOhjshAYqg1R7fkqvWbH__BS4umfrERA/s843/librephyton.jpg&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;529&quot; data-original-width=&quot;843&quot; height=&quot;402&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjo1aSyEUQe63CHcsX5YLhodaWUN2fKwusORdSW9rW9Xi5vJwSTYG3Z37wFr80ZEhGJg01K8hx5pKATdgFROmfaMm_oVqpZvwcc3YWHLwrRgNDAnB9_PyJPgONPGXuu77jyOCLQAfpPoVKoWQBsNlAQbOhjshAYqg1R7fkqvWbH__BS4umfrERA/w640-h402/librephyton.jpg&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;Tela do LibreOffice Calc com Python&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Pré-requisitos&lt;/h2&gt;&lt;/div&gt;&lt;div&gt;&lt;a href=&quot;https://www.libreoffice.org/&quot; target=&quot;_blank&quot;&gt;LibreOffice&lt;/a&gt; e Python instalados. Tudo vai rodar no seu PC. Não vamos abordar essas instalações aqui.&lt;/div&gt;&lt;div&gt;Iremos tratar apenas da instalação e configuração da extensão &lt;i&gt;LibrePythonista&lt;/i&gt;.&lt;/div&gt;&lt;div&gt;Precisamos também das bibliotecas &lt;i&gt;python-dotenv&lt;/i&gt; e &lt;i&gt;openai&lt;/i&gt;, as quais mostraremos mais adiante como instalá-las.&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Instalando a extensão&lt;/h2&gt;&lt;div&gt;Inicialmente, acesse o &lt;a href=&quot;https://extensions.libreoffice.org/&quot; target=&quot;_blank&quot;&gt;repositório de extensões do LibreOffice&lt;/a&gt;, digite LibrePythonista na caixa de pesquisa e clique no botão &lt;i&gt;Search&lt;/i&gt;. Você verá esta tela:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;border: 1px solid rgb(0, 0, 0); clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEidyCH2D-6vByCrO6Ehu5KTFVHkkwVeL38dXP_ntlFP8k4_iKPocM0Wmt-Af0324SXMLWaGiAj451EkDwaiMCC-t7qjdTXaL7NfzlbLVIrng92ACRcdojSo9ZEIljkNlmEPqgXmCPah2EKkjfS2hKDfqvLACzPZdHIJIiBXrW3m2x5_Sm_UZdzP/s1535/Captura%20de%20tela%20de%202025-12-15%2017-50-50.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;826&quot; data-original-width=&quot;1535&quot; height=&quot;344&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEidyCH2D-6vByCrO6Ehu5KTFVHkkwVeL38dXP_ntlFP8k4_iKPocM0Wmt-Af0324SXMLWaGiAj451EkDwaiMCC-t7qjdTXaL7NfzlbLVIrng92ACRcdojSo9ZEIljkNlmEPqgXmCPah2EKkjfS2hKDfqvLACzPZdHIJIiBXrW3m2x5_Sm_UZdzP/w640-h344/Captura%20de%20tela%20de%202025-12-15%2017-50-50.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style=&quot;text-align: center;&quot;&gt;Tela de&amp;nbsp;https://extensions.libreoffice.org/&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;Clique no card da biblioteca e em seguida no botão &lt;i&gt;Download latest&lt;/i&gt;. Agora abra o Calc e vá em &lt;i&gt;Ferramentas -&amp;gt; Extensões&lt;/i&gt;. Você verá uma janela com as extensões instaladas. Clique em &lt;i&gt;Adicionar&lt;/i&gt; e procure o arquivo &lt;i&gt;LibrePythonista.oxt&lt;/i&gt; na pasta de downloadas do seu computador. Aceite a licença e quando a instalação finalizar concorde em reiniciar o Calc. Agora você verá o menu &lt;i&gt;LibrePy&lt;/i&gt;. Nesse processo de instalação, alguns pacotes são instalados por padrão: &lt;i&gt;pandas&lt;/i&gt; e &lt;i&gt;matplotlib&lt;/i&gt;, por exemplo.&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;O último passo antes de ver a extensão em ação é configurar a segurança de macros. É necessário configurar de modo que o Calc permita a execução de macros. O padrão é que não execute. Então vá em &lt;i&gt;Ferramentas -&amp;gt; Opções&lt;/i&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;border: 1px solid rgb(0, 0, 0); text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0h0Aw6mP_1jUD9XXJhASnQvN-nYkZjhJrKtSTI7FvLwMxyenT1VC_RVV7zBOYsYj4w7P6cPJ83WBsK1B39JNz-rY6IwrCiS_1lL_ae6ojblYFW6woCQNoaEJu2kYRb0U5c2Kh_ncl6nt_bru5GCuIuikJWZa3jRkZRsYmnqFyO117IxpMh5Gc/s1535/Captura%20de%20tela%20de%202025-12-15%2018-15-51.png&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;830&quot; data-original-width=&quot;1535&quot; height=&quot;346&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0h0Aw6mP_1jUD9XXJhASnQvN-nYkZjhJrKtSTI7FvLwMxyenT1VC_RVV7zBOYsYj4w7P6cPJ83WBsK1B39JNz-rY6IwrCiS_1lL_ae6ojblYFW6woCQNoaEJu2kYRb0U5c2Kh_ncl6nt_bru5GCuIuikJWZa3jRkZRsYmnqFyO117IxpMh5Gc/w640-h346/Captura%20de%20tela%20de%202025-12-15%2018-15-51.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Tela de configutação de segurança de macros&lt;br /&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;Clique em &lt;i&gt;Segurança&lt;/i&gt; e em &lt;i&gt;Segurança de macros&lt;/i&gt;. Selecione a segurança &lt;i&gt;Média&lt;/i&gt;, a qual exibe uma caixa de diálogo pede a confirmação do usuário antes da execução do script. Daí é só confirmar.&lt;h2 style=&quot;text-align: left;&quot;&gt;O primeiro script&lt;/h2&gt;&lt;div&gt;Com o propósito de ver se está tudo certo. Digite, por exemplo, 10 na célula A1, 20 na célula B1 e, com C1 selecionada, clique no menu &lt;i&gt;LibrePy -&amp;gt; Inserir Python&lt;/i&gt;. Ou use o atalho SHIFT+CTRL+ALT+L. Um botão será inserido em C1, indicando que esta célula tem script Python.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;border: 1px solid rgb(0, 0, 0); clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUpGEz8KbLy8jyRJgOCNMWj7DoRnO133tw_MOct2xNKOc0jtWemxWgv8ciQrBGxvBBY36d6zuMexOKQSBei2Te8tENLRTNEb8N3F3sXitOfloE4_hFbuY1NLyMGB0z7zIa05snUDOOxdeXYI2eGkS3lSsX0zNU0HJZ0lvEViZIXaguVG8nt7u1/s893/Captura%20de%20tela%20de%202025-12-15%2018-27-49.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;553&quot; data-original-width=&quot;893&quot; height=&quot;396&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjUpGEz8KbLy8jyRJgOCNMWj7DoRnO133tw_MOct2xNKOc0jtWemxWgv8ciQrBGxvBBY36d6zuMexOKQSBei2Te8tENLRTNEb8N3F3sXitOfloE4_hFbuY1NLyMGB0z7zIa05snUDOOxdeXYI2eGkS3lSsX0zNU0HJZ0lvEViZIXaguVG8nt7u1/w640-h396/Captura%20de%20tela%20de%202025-12-15%2018-27-49.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;Clique no botão dentro da célula e em &lt;i&gt;Editar Código&lt;/i&gt; e o editor irá abrir. Digite o código que aparece na imagem e clique em &lt;i&gt;OK&lt;/i&gt;. O resultado será exibido na célula C1. A função &lt;i&gt;lp()&lt;/i&gt; pega o valor de uma célula e o atribui a uma variável. O restante do script é Python, sendo que na última linha digitamos a variável que será exibida na célula. Quando salvamos a planilha, o script é salvo também.&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Pronto. A extensão está instalada, configurada e funcionando. Mas o que queremos é utilizar o Calc como nossa ferramenta para controle de finanças pesoais, a exemplo que temos feito com Google Sheets. Mais especificamente, queremos acessar o ChatGPT a partir de uma célula a fim de registrar transações e de obter&amp;nbsp; diagnóstico e insights sobre nossas finanças.&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Acessando o ChatGPT&lt;/h2&gt;&lt;div&gt;A primeira providencia a tomar a fim de ter acesso à IA é guardar o token da API da OpenAI em lugar seguro. E temos duas opções:&lt;/div&gt;&lt;div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Criar uma variável de ambiente no sistema operacional e manter aí o token. O procedimento para isso depende do sistema onde o LibreOffice está executando.&lt;/li&gt;&lt;li&gt;Utiizar a biblioteca &lt;i&gt;python-dotenv&lt;/i&gt;, conforme já mostramos &lt;a href=&quot;https://professorcarlos.blogspot.com/2025/11/como-enviar-notificacoes-por-e-mail-ou-pelo-telegram.html&quot; target=&quot;_blank&quot;&gt;neste artigo&lt;/a&gt;. Neste caso, precisamos manter a chave da API num arquivo denominado &lt;i&gt;.env&lt;/i&gt;.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;
&lt;div&gt;Vamos adotar a segunda opção, a qual funciona em qualquer sistema operacional. Para isso, crie o arquivo &lt;i&gt;.env&lt;/i&gt; com a chave&amp;nbsp;&lt;i&gt;OPENAI_API_KEY=sua_chave_da_API_OpenAI&lt;/i&gt;, por exemplo, e salve-o em uma pasta apropriada. Eu salvei no mesmo diretório onde salvei a planilha.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Na planilha, eu quero solicitar ao ChatGPT que leia uma frase que está na célula A1, interprete e extraia dados de uma transação financeira e retorne um JSON com esses dados. O script estará na célula B1.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Para isso, nós precisamos de duas bibliotecas externas: &lt;i&gt;python-dotenv&lt;/i&gt;, reponsável para ler variáveis de ambiente, no caso &lt;i&gt;OPEN_API_KEY&lt;/i&gt;, e &lt;i&gt;openai&lt;/i&gt;, para acesso a modelos de IA da OpenAI.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;A fim de instalar esses pacotes, abra o menu &lt;i&gt;LibrePy -&amp;gt; Pip -&amp;gt; Instalar pacote Pip&lt;/i&gt;. Na janela que será aberta digite o nome do pacote, &lt;i&gt;python-dotenv&lt;/i&gt;, e clique em &lt;i&gt;OK&lt;/i&gt;. Repita o procedimento para instalar o pacote &lt;i&gt;openai&lt;/i&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Agora selecione a célula B1 e clique no menu&amp;nbsp;&lt;i&gt;LibrePy -&amp;gt; Inserir Python&lt;/i&gt;. Ou use o atalho SHIFT+CTRL+ALT+L. O controle será exibido na célula, indicando que ela contém um script Python. clique no controle e em &lt;i&gt;Editar Código&lt;/i&gt;. Digite o código no editor:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; os&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; json&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; openai &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;OpenAI&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; dotenv &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;load_dotenv&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; datetime &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;date&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;load_dotenv(dotenv_path&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;/home/carlos/development/libreoffice-com-ia/.env&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;API_KEY&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;os&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;getenv(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;OPENAI_API_KEY&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;client&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;OpenAI(api_key&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;API_KEY)&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;mensagem&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;lp(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;A1&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;response&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;client&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;chat&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;completions&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;create(&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;model&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;gpt-3.5-turbo&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;messages&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;[&lt;/span&gt;
                &lt;span style=&quot;color: #f8f8f2;&quot;&gt;{&lt;/span&gt;
                    &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;system&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
                    &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;&quot;&quot;Você é um sistema de categorização de&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                        transações financeiras. Responda APENAS em formato&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                        JSON válido, sem explicações adicionais.&quot;&quot;&quot;&lt;/span&gt;
                &lt;span style=&quot;color: #f8f8f2;&quot;&gt;},&lt;/span&gt;
                &lt;span style=&quot;color: #f8f8f2;&quot;&gt;{&lt;/span&gt;
                    &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;user&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
                    &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;&quot;&quot;Extraia da frase abaixo:&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                        - valor (float, usar . como separador decimal)&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                        - tipo: &quot;Receita&quot; ou &quot;Despesa&quot;&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                        - categoria (uma palavra)&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                        - data (DD/MM/YYYY; se não informado,&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                            usar {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;today()&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;strftime(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;%d/%m/%Y&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;})&lt;/span&gt;

&lt;span style=&quot;color: #bd93f9;&quot;&gt;                    Frase: &quot;{&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;mensagem&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&quot;&lt;/span&gt;

&lt;span style=&quot;color: #bd93f9;&quot;&gt;                    Retorne APENAS JSON, exemplo:&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                        {{&quot;valor&quot;: 58,.0, &quot;tipo&quot;: &quot;Despesa&quot;,&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                            &quot;categoria&quot;: &quot;Alimentacao&quot;,&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                            &quot;data&quot;: &quot;28/11/2025&quot;}}&quot;&quot;&quot;&lt;/span&gt;
                &lt;span style=&quot;color: #f8f8f2;&quot;&gt;}&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;],&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;max_tokens&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;800&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;temperature&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0.3&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;texto&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;response&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;choices[&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;message&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;content&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;strip()&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;texto&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Inicialmente importamos as bibliotecas necessárias, e em seguida carregamos o arquivo &lt;i&gt;.env&lt;/i&gt; informando o caminho absoluto na função &lt;i&gt;load_dotenv()&lt;/i&gt;. Assegure-se de informar o caminho correto para o seu arquivo &lt;i&gt;.env&lt;/i&gt;. Feito isso, lemos o token e inicializamos um cliente OpenAI. Depois, a frase é lida da célula A1. Estamos utilizando o mesmo prompt já usado em artigos passados para chamar o método &lt;i&gt;chat.completions.create()&lt;/i&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;A chamada retorna um &lt;i&gt;response&lt;/i&gt;, e o conteúdo mesmo da resposta é extraido para a variável texto. Daí temos um &lt;i&gt;json&lt;/i&gt; exibido na célula em formato string. Observe que na caixa de entrada referente à célula com o código Python, há a seguinte fórmula: &lt;i&gt;=PY.C(PLANILHA();CÉL(&quot;ADDRESS&quot;);&lt;b&gt; A1&lt;/b&gt;)&lt;/i&gt;. O último parâmetro indica uma célula ou intervalo, o qual dispara a execução do script, caso seja modificado. Assim, informamos A1 neste caso.&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Nosso objetivo é preencher cada célula subsequente, na mesma linha, com os valores retornados no &lt;i&gt;json&lt;/i&gt;. Entretanto, não vimos, na extensão &lt;i&gt;LibrePythonista&lt;/i&gt;, uma maneira de o script preencher células diferentes daquela onde o script está executando. Por esse motivo, iremos partir para uma solução alternativa.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Insira Python na célula C1 e digite o seguinte código:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; json&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;texto&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;lp(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;B1&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;texto_json&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;json&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;loads(texto)&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;valor&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;texto_json[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;valor&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;valor&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;O que fazemos aqui é pegar o conteúdo de B1 (resposta da IA) e exibir &lt;i&gt;valor&lt;/i&gt; na célula. Defina a fórmula na caixa de entrada como&amp;nbsp;&lt;i&gt;=PY.C(PLANILHA();CÉL(&quot;ADDRESS&quot;);B1)&lt;/i&gt;, de modo que C1 seja atualizada sempre que B1 se modificar. Repita isso para &lt;i&gt;categoria&lt;/i&gt; e &lt;i&gt;tipo&lt;/i&gt; nas células D1 e E1, respectivamente. Finalmente, podemos ocultar a coluna B1.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Outra limitação com a qual me deparei é que ao copiar uma célula para outra, o script não é copiado. Então, temos que repetir todo esse processo nas demais linhas.&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Solução alternativa usando Python com &lt;i&gt;ScriptForge&lt;/i&gt;&lt;/h2&gt;&lt;div&gt;Essa parte final, onde os valores do &lt;i&gt;json&lt;/i&gt; são escritos nas células seguintes, pode ser feita com uma macro Python usando &lt;i&gt;ScriptForge&lt;/i&gt;, a qual é uma biblioteca de ferramentas que simplifica o desenvolvimento de macros e scripts em Python e LibreOffice Basic.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Vamos instalar a extensão&amp;nbsp;&lt;i&gt;APSO&lt;/i&gt; (Alternative Python Script Organizer), que simplifica um pouco mais a criação e organização das macros Python. O processo de instalação é o mesmo que fizemos para instalar a &lt;i&gt;LibrePythonista&lt;/i&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Antes de prosseguir é recomendado configurar o editor de sua preferência. Vá em &lt;i&gt;Ferramentas -&amp;gt; Extensões&lt;/i&gt;. Clique na extensão &lt;i&gt;APSO&lt;/i&gt; e em &lt;i&gt;Opções&lt;/i&gt;. Digite o caminho completo do editor na caixa de texto ou clique no botão &lt;i&gt;Escolher&lt;/i&gt; para selecionar pelo navegador de pastas. Com isso, o menu &lt;i&gt;Editar&lt;/i&gt; na janela do &lt;i&gt;APSO&lt;/i&gt; irá abrir o editor que você definiu.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Outro ponto a se preocupar é onde as macros serão salvas. Em geral, o LibreOffice procura as macros em um diretório no perfil do usuário:&lt;/div&gt;&lt;div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;No &lt;b&gt;Windows&lt;/b&gt;:&amp;nbsp;%appdata%\libreoffice\4\user\scripts\python&lt;/li&gt;&lt;li&gt;No &lt;b&gt;Linux&lt;/b&gt;:&amp;nbsp;~/.config/libreoffice/4/user/scripts/python&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;Se as pastas &lt;i&gt;scripts&lt;/i&gt; e &lt;i&gt;python&lt;/i&gt; não existirem, você precisa criá-las.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Agora, vá em &lt;i&gt;Ferramentas -&amp;gt; Macros -&amp;gt; Organizar scripts em Python&lt;/i&gt;. Esta janela será aberta:&lt;/div&gt;&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhT8ffN0HWguj8IFGHGItAxLqtdGtvYIhPpFO_pFSZxNlgrjT9-Dj6P2SdHr03M3S8I62bwPcJsy2d92STBuGJgm2WRKJN8zhGxak3huNl1h0adftf1UA4EumeuSv5LwOQAauwZzeMO0FdA15hQigA6BJHobobD4DyqgVATixEt3LeidAkGH8k_/s424/Captura%20de%20tela%20de%202025-12-17%2017-41-17.png&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;424&quot; data-original-width=&quot;352&quot; height=&quot;400&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhT8ffN0HWguj8IFGHGItAxLqtdGtvYIhPpFO_pFSZxNlgrjT9-Dj6P2SdHr03M3S8I62bwPcJsy2d92STBuGJgm2WRKJN8zhGxak3huNl1h0adftf1UA4EumeuSv5LwOQAauwZzeMO0FdA15hQigA6BJHobobD4DyqgVATixEt3LeidAkGH8k_/w333-h400/Captura%20de%20tela%20de%202025-12-17%2017-41-17.png&quot; width=&quot;333&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Tela do APSO&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;div&gt;Com &lt;i&gt;Minhas Macros&lt;/i&gt; selecionada, clique em &lt;i&gt;Menu&lt;/i&gt; e &lt;i&gt;Criar Módulo&lt;/i&gt;. Informe um nome para o módulo. Por exemplo &lt;i&gt;minhas_macros.py&lt;/i&gt;&amp;nbsp;e confirme. Agora, clique no nome do módulo e em &lt;i&gt;Menu -&amp;gt; Editar&lt;/i&gt;. Digite este código no arquivo criado:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px; text-align: left;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; json&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; scriptforge &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;CreateScriptService&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;doc&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;CreateScriptService(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Calc&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;fill_cells&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(args&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=None&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;):&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;value&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;doc&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;GetValue(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;B1&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;texto_json&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;json&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;loads(value)&lt;/span&gt;

    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;doc&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;SetValue(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;F1&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;texto_json[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;valor&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;])&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;doc&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;SetValue(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;G1&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;texto_json[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;categoria&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;])&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;doc&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;SetValue(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;H1&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;texto_json[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;tipo&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;])&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;g_exportedScripts&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;(fill_cells,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;Após os &lt;i&gt;imports&lt;/i&gt; necessários, o primeiro passo é definir que estaremos lidando com uma planilha do Calc. Em seguida definimos uma função denominada &lt;i&gt;fill_cells()&lt;/i&gt;. Ela irá aparecer na janela do &lt;i&gt;APSO&lt;/i&gt; e dará a opção de ser executada.&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;Depois, o valor retornado pelo ChatGPT é lido e convertido em &lt;i&gt;json&lt;/i&gt;. E cada um dos campos é escrito numa célula.&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;Finalmente, nós exportamos as funções que serão nossos scripts a ser executados. Note que, na imagem da janela do &lt;i&gt;APSO&lt;/i&gt;, &lt;i&gt;fill_cells&lt;/i&gt; aparece abaixo de &lt;i&gt;arquivo&lt;/i&gt;, que foi o nome que dei ao meu módulo. Note também que, se você clicar em &lt;i&gt;fill_cells&lt;/i&gt;, o botão &lt;i&gt;Executar&lt;/i&gt; é habilitado. Clique nele para rodar o script. E, pronto, os valores preenchem as células F1, G1 e H1, conforme programamos no script.&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Conclusão&lt;/h2&gt;&lt;div&gt;A extensão &lt;i&gt;LibrePythonista&lt;/i&gt; ainda está em beta. Possui muitas limitações. No entanto, podemos nos beneficiar do uso de IA para tarefas mais esporádicas, tais como analisar uma planilha de finanças pessoais e nos dar alguns insights que podem nos ajudar a economizar e usar mais racionalmente o nosso dinheiro.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Alternativamente, podemos criar esse script usando &lt;i&gt;ScriptForge&lt;/i&gt; e Python. O uso de pacotes externos pode ser um desafio, mas está em beta na extensão &lt;i&gt;APSO&lt;/i&gt; a opção de gerenciar pacotes &lt;i&gt;pip&lt;/i&gt;, o que pode ser de grande ajuda. No entanto, apesar das limitações, &lt;i&gt;LibrePythonista&lt;/i&gt; oferece uma fórmula que possibilita que o script seja executado sempre que uma célula ou intervalo de células seja modificado. A execução de scripts &lt;i&gt;ScriptForge&lt;/i&gt; com Python a partir de atualizações de células parece ser bastante desafiador.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;De todo modo, num próximo post, iremos tratar como fazer esta integração usando &lt;i&gt;ScriptForge&lt;/i&gt; e Python.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Se você tem interesse em outras soluções de macros em Basic ou Python para LibreOffice dê uma olhada em&amp;nbsp;&lt;a href=&quot;https://stonefull.gumroad.com/l/financial_data_chatgpt_libreoffice&quot;&gt;https://stonefull.gumroad.com/l/financial_data_chatgpt_libreoffice&lt;/a&gt;.&amp;nbsp;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://professorcarlos.blogspot.com/feeds/5561293583107148636/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/33774862/5561293583107148636?isPopup=true' title='0 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/5561293583107148636'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/5561293583107148636'/><link rel='alternate' type='text/html' href='http://professorcarlos.blogspot.com/2025/12/integrando-python-e-chatgpt-com-libreoffice-calc.html' title='Integrando Python e ChatGPT com LibreOffice Calc'/><author><name>Professor Carlos</name><uri>http://www.blogger.com/profile/10347668022241306511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFBZD8558AE67R-CINkytW3INbUINaAhDuZ7GKm_4DSSKeHSpdm3K-TPQrhNPn5B-21wN24ZH8flimqC7ikTzmRrYZBAsyD_JSC8YW8BDJZwmTby9R7LZC9SYkyHkSs7NPVZxXiqEDqjhkRJNxZwqqyZaVgYjISiFpzXsSyGZmL1YFCQ/s220/WhatsApp%20Image%202024-05-17%20at%2017.42.05.jpeg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjo1aSyEUQe63CHcsX5YLhodaWUN2fKwusORdSW9rW9Xi5vJwSTYG3Z37wFr80ZEhGJg01K8hx5pKATdgFROmfaMm_oVqpZvwcc3YWHLwrRgNDAnB9_PyJPgONPGXuu77jyOCLQAfpPoVKoWQBsNlAQbOhjshAYqg1R7fkqvWbH__BS4umfrERA/s72-w640-h402-c/librephyton.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-33774862.post-1143860538802948421</id><published>2025-12-12T19:02:00.001-03:00</published><updated>2025-12-12T21:06:28.341-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="apps script"/><category scheme="http://www.blogger.com/atom/ns#" term="automação"/><category scheme="http://www.blogger.com/atom/ns#" term="google sheets"/><category scheme="http://www.blogger.com/atom/ns#" term="IA"/><category scheme="http://www.blogger.com/atom/ns#" term="inteligência artificial"/><title type='text'>Como Integrar ChatGPT com Google Sheets usando Google Apps Script</title><content type='html'>&lt;h1 style=&quot;text-align: left;&quot;&gt;Como Integrar ChatGPT com Google Sheets usando Google Apps Script&lt;/h1&gt;&lt;div&gt;Vimos no &lt;a href=&quot;https://professorcarlos.blogspot.com/2025/12/chatgpt-google-sheets-combinacao-perfeita-para-controlar-seus-gastos-automaticamente.html&quot; target=&quot;_blank&quot;&gt;post anterior&lt;/a&gt; como integrar ChatGPT com Google Sheets utilizando uma extensão. Ao usar uma extensão ficamos limitados às funções que a ferramenta oferece, e isso pode não ser o melhor dos mundos ao criar uma planilha.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Neste artigo iremos introduzir uma alternativa ao uso de extensões. Vamos programar nossas próprias funções, adequando-as às nossas necessidades. Para isso usaremos o Apps Script, uma plataforma JavaScript baseada na nuvem com a tecnologia Google Drive que permite integração e automação de tarefas nos produtos do Google, não apenas no Sheets.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVsSQMBHNgt6QAtYQOtVL-1RCrj3wX-aTP4pVW6fRNvdpCyCG2rZZ727HN3-TXI9kNHVhGAqmus-SS1dUpbtOcepzucSi1roWzu5oj6J49XtvuAt-rvCm_TXhRG5FYnMwFdg-CfliuQGVwDUioiiUcFZu6s11UEAGI4OBZnzeaZVxZXTBHG5EX/s1503/Untitled%20design%20(7)%20(1).png&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;1068&quot; data-original-width=&quot;1503&quot; height=&quot;454&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVsSQMBHNgt6QAtYQOtVL-1RCrj3wX-aTP4pVW6fRNvdpCyCG2rZZ727HN3-TXI9kNHVhGAqmus-SS1dUpbtOcepzucSi1roWzu5oj6J49XtvuAt-rvCm_TXhRG5FYnMwFdg-CfliuQGVwDUioiiUcFZu6s11UEAGI4OBZnzeaZVxZXTBHG5EX/w640-h454/Untitled%20design%20(7)%20(1).png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Fonte: https://br.freepik.com/&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Pré-requisitos&lt;/h2&gt;&lt;div&gt;Será necessário que você tenha um token da API OpenAI. &lt;a href=&quot;https://professorcarlos.blogspot.com/2025/11/seu-assistente-financeiro-pessoal-no-telegram-integrando-ia-ao-bot-do-google-sheets.html&quot; target=&quot;_blank&quot;&gt;Neste artigo&lt;/a&gt; nós mostramos como obtê-lo. Reforçando que o uso desta API tem um custo e devemos estar cientes disso.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;É necessário conhecimento de JavaScript. Não está no escopo deste post introduzir o leitor nesta linguagem. Se for necessário estudar os fundamentos de JavaScript, recomendo o&amp;nbsp;&lt;a href=&quot;https://www.w3schools.com/js/&quot; target=&quot;_blank&quot;&gt;tutorial&lt;/a&gt;&amp;nbsp;da W3School.&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;O que será criado&lt;/h2&gt;&lt;div&gt;Vamos criar uma função personalizada no Google Sheets, que usa o ChatGPT para extrair dados de uma transação informada pelo usuário na forma textual. Por exemplo: &lt;i&gt;98 reais remédios&lt;/i&gt;. Essa frase será digitada na coluna Descrição. E o script criado por nós estará numa opção de menu, e ao ser executado, irá analisar a frase e preencher as demais colunas da linha: Data, Valor, Categoria e Tipo.&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Primeiro passo&lt;/h2&gt;&lt;div&gt;Inicialmente crie uma planilha com as seguintes colunas: Descrição, Data, Valor, Categoria e Tipo. Nomeie a planilha como &lt;i&gt;Finanças Pessoais&lt;/i&gt; e a página como &lt;i&gt;Transações&lt;/i&gt;, por exemplo. Você pode atribuir qualquer nome. Em seguida clique no item de menu&amp;nbsp;&lt;i&gt;Extensões -&amp;gt; Apps Script&lt;/i&gt;. Será aberta uma nova aba no navegador com esta aparência:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;border: 1px solid rgb(0, 0, 0); text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3ZLtgt_LphdDepSs_X1FZJ-6vYsLZ4ncDfy1oYj43GJjyhcwt6kFhbVULOWR4IgP0s27i2RQO9hvupJP40_PR8FqnTuCusGxG2sHLjhuabUYMSg_Z9_BDblkbdZL5l-kAa1evGh6U-9iM0TiO_MDGQm0_jzQA2_o4GgcdyVlgo59eCr9U-LKG/s952/Captura%20de%20tela%20de%202025-12-11%2017-36-41.png&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;531&quot; data-original-width=&quot;952&quot; height=&quot;356&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi3ZLtgt_LphdDepSs_X1FZJ-6vYsLZ4ncDfy1oYj43GJjyhcwt6kFhbVULOWR4IgP0s27i2RQO9hvupJP40_PR8FqnTuCusGxG2sHLjhuabUYMSg_Z9_BDblkbdZL5l-kAa1evGh6U-9iM0TiO_MDGQm0_jzQA2_o4GgcdyVlgo59eCr9U-LKG/w640-h356/Captura%20de%20tela%20de%202025-12-11%2017-36-41.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Tela do Projeto Apps Script vinculado à planilha&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;div&gt;Clique em &lt;i&gt;Projeto sem título&lt;/i&gt; e renomeie-o. Por exemplo, pode dar o nome de &lt;i&gt;ChatGPT for Sheets&lt;/i&gt;. E agora estamos prontos para criar nossa função. No entanto, antes de começar, vamos inserir o token da API OpenAI. Para isso, clique na engrenagem à esquerda: &lt;i&gt;Configurações do projeto&lt;/i&gt;. Role a tela até encontrar &lt;i&gt;Propriedades do script&lt;/i&gt;&amp;nbsp;e clique em &lt;i&gt;Adicionar propriedade do script&lt;/i&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;border: 1px solid rgb(0, 0, 0); text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQx4G08nqaefESMabppKvjglb3YxN74BDVkR2Ebvz_ypEGS421p96yw1ZEns3FiPcOLRa8IeEpm25O41_BCEoB8-RTAxXuKdEv-SFdvGe94yJnmVtIs4dMgXPwXzPDkdK1GbfI7yU81TlwsC1f3tRmVhoRBUuYn5a0gVEnHHhvPFx-1v72Y8hyphenhyphen/s959/Captura%20de%20tela%20de%202025-12-11%2018-03-48.png&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;727&quot; data-original-width=&quot;959&quot; height=&quot;486&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQx4G08nqaefESMabppKvjglb3YxN74BDVkR2Ebvz_ypEGS421p96yw1ZEns3FiPcOLRa8IeEpm25O41_BCEoB8-RTAxXuKdEv-SFdvGe94yJnmVtIs4dMgXPwXzPDkdK1GbfI7yU81TlwsC1f3tRmVhoRBUuYn5a0gVEnHHhvPFx-1v72Y8hyphenhyphen/w640-h486/Captura%20de%20tela%20de%202025-12-11%2018-03-48.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Tela para adicionar propriedade&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;div&gt;Digite o nome da propriedade na caixa &lt;i&gt;Propriedade&lt;/i&gt;: API_TOKEN, por exemplo, e cole o token na caixa &lt;i&gt;Valor&lt;/i&gt;. Agora, clique no botão &lt;i&gt;Salvar propriedades do script&lt;/i&gt;. Com isso, nós evitamos expor o token no código do script, o que é uma prática recomendada. Para retornar ao script, clique no ícone &lt;i&gt;Editor&lt;/i&gt; à esquerda.&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Segundo passo&lt;/h2&gt;&lt;div&gt;A seguir vamos iniciar a criação do script, explicando o conceito de &lt;i&gt;namespace&lt;/i&gt;. &lt;i&gt;Namespace,&lt;/i&gt; no contexto de linguagens de programação&lt;i&gt;,&lt;/i&gt; é um espaço nomeado onde podemos declarar variáveis, objetos, classes, funções, etc., os quais estão fora do escopo global. Para acessar qualquer desses elementos precisamos utilizar primeiro o nome do espaço, seguido do identificador do elemento. Estamos usando esse recurso para prevenir que funções internas ao script sejam acessadas em uma célula da planilha.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Antes de digitar qualquer linha de código, delete as linhas existentes no editor e digite o seguinte trecho. Esse é o código da função que lê o token da API armazenado nas propriedades do script:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; Utils &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  getApiKey&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;() {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; scriptProperties &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; PropertiesService.getScriptProperties();&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; apiKey &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; scriptProperties.getProperty(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;API_TOKEN&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; apiKey;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  }&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Aqui definimos &lt;i&gt;Utils&lt;/i&gt; como um &lt;i&gt;namespace&lt;/i&gt; e escrevemos as funções que desejamos sejam privadas dentro desse espaço. &lt;i&gt;Namespace&lt;/i&gt; é um objeto JavaScript, onde cada elemento é definido na forma chave/valor. Neste caso, &lt;i&gt;getApiKey&lt;/i&gt; é a chave, e o valor é a declaração da função. Basicamente, estamos usando a classe &lt;i&gt;PropertiesService&lt;/i&gt; para obter o valor da propriedade &lt;i&gt;API_TOKEN&lt;/i&gt;, que definimos anteriormente. Além de ler uma propriedade, esta classe também pode gravar novas propriedades, mas isso não nos interessa agora.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Em seguida, escrevemos a função que faz a requisição à API da OpenAI, dentro do &lt;i&gt;namespace&lt;/i&gt;. Esta função recebe o prompt do sistema e do usuário, faz a requisição e retorna a mensagem respondida pelo modelo. Assim, você pode criar depois outras funções customizadas, além desta que analisa o texto da transação. Apenas defina os prompts e chame &lt;i&gt;getResponseAI()&lt;/i&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; Utils &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  getApiKey&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;() {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; scriptProperties &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; PropertiesService.getScriptProperties();&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; apiKey &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; scriptProperties.getProperty(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;API_TOKEN&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; apiKey;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  },&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  getResponseAI&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(system_prompt,user_prompt) {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; requestOptions &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;      &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;method&quot;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;POST&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;      &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;headers&quot;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Content-Type&quot;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;application/json&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Authorization&quot;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;`Bearer ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Utils.getApiKey()&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}`&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;      },&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;      &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;payload&quot;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;JSON&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;.stringify({&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;model&quot;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;gpt-4.1&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;input&quot;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; user_prompt,&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;instructions&quot;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; system_prompt,&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;        &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;max_output_tokens&quot;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;500&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;      })&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    };&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; response &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; UrlFetchApp.fetch(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;https://api.openai.com/v1/responses&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;, requestOptions);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; (response.getResponseCode() &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;!==&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;200&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;) {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;      Logger.log(response.getContentText());&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;      &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;throw&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; Error(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Falha na requisição à IA: &quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; response.getContentText());&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    }&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    response &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;JSON&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;.parse(response);&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; response.output[&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;].content[&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;].text;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  }&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Note que, antes de iniciar a nova função, foi inserida uma vírgula após o elemento anterior. Isso é parte da sintaxe do objeto JavaScript.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Diferente do que vimos no artigo&amp;nbsp;&lt;a href=&quot;https://professorcarlos.blogspot.com/2025/11/seu-assistente-financeiro-pessoal-no-telegram-integrando-ia-ao-bot-do-google-sheets.html&quot; target=&quot;_blank&quot;&gt;Seu Assistente Financeiro Pessoal no Telegram: Integrando IA ao Bot do Google Sheets&lt;/a&gt;, onde usamos uma biblioteca Python para acessar a API OpenAI, aqui não temos intermediário. A requisição (&lt;i&gt;fetch&lt;/i&gt;) é feita diretamente ao &lt;i&gt;endpoint&lt;/i&gt; da API, informando as opções da requisição, de acordo com as &lt;a href=&quot;https://platform.openai.com/docs/api-reference/responses&quot; target=&quot;_blank&quot;&gt;instruções da documentação&lt;/a&gt;.&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Função personalizada&lt;/h3&gt;&lt;div&gt;Mesmo não sendo a solução que iremos adotar neste exemplo, vamos apresentar uma função personalizada que interpreta a frase e retorna os valores da transação: Data, Valor, Categoria e Tipo.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;/**&lt;/span&gt;
&lt;span style=&quot;color: #6272a4;&quot;&gt; * Interpreta uma transação em forma textual.&lt;/span&gt;
&lt;span style=&quot;color: #6272a4;&quot;&gt; * @customfunction&lt;/span&gt;
&lt;span style=&quot;color: #6272a4;&quot;&gt;*/&lt;/span&gt;
&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; INTERPRETAR(text) {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; (text &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;===&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; text &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;===&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;) {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; [[]];&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  }&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; system_prompt &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Você é um sistema de categorização de transações financeiras. Responda APENAS em formato JSON válido, sem explicações adicionais.&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; user_prompt &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;`Extraia da frase abaixo:&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                        - valor (float, usar . como separador decimal)&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                        - tipo: &quot;Receita&quot; ou &quot;Despesa&quot;&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                        - categoria (uma palavra)&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                        - data (DD/MM/YYYY; se não informado,&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                           usar ${&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;Date&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;})&lt;/span&gt;

&lt;span style=&quot;color: #bd93f9;&quot;&gt;                    Frase: ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: #bd93f9;&quot;&gt;                    Retorne APENAS JSON, exemplo:&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                        {{&quot;valor&quot;: 58,.0, &quot;tipo&quot;: &quot;Despesa&quot;,&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                            &quot;categoria&quot;: &quot;Alimentacao&quot;,&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                            &quot;data&quot;: &quot;28/11/2025&quot;}}`&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  json_response &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;JSON&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;.parse(Utils.getResponseAI(system_prompt, user_prompt));&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; [[json_response.data, json_response.valor, json_response.categoria, json_response.tipo]];&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;A função &lt;i&gt;INTERPRETAR()&lt;/i&gt; fica após o &lt;i&gt;namespace Utils&lt;/i&gt;. Ou seja, fora do escopo do &lt;i&gt;namespace&lt;/i&gt;. Uma função personalizada precisa ter a anotação &lt;i&gt;@customfunction&lt;/i&gt; a fim de que apareça na complementação quando digitamos = numa célula. Mas não é obrigatório. Bem como não é obrigatório que nome da função esteja em caixa alta. Apenas seguimos o padrão de nomes das funções.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Se o retorno de uma função customizada for um único valor, este é lançado na célula corrente. Se for um array, os valores deste são lançados na coluna a partir da célula corrente. Se retornar um array de arrays, então um trecho de tabela é lançado a partir da célula corrente, onde cada elemento array compõe uma linha.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;No nosso caso queremos lançar os dados da transação a partir da coluna Data, por isso retornamos um array de array. Logo no início da função &lt;i&gt;INTERPRETAR()&lt;/i&gt;, que recebe como parâmetro o nome da célula onde está a frase, é verificado se o conteúdo dessa célula é vazio. Caso seja, a função retorna um array de array vazio.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Em seguida são criados os prompts, tanto de sistema quanto de usuário, e estes são passados para &lt;i&gt;Utils.getResponseAI()&lt;/i&gt;, a qual retorna um JSON, de acordo com as instruções nos prompts.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;E porque não adotamos essa função como solução para o nosso problema? Dois motivos são determinantes:&lt;/div&gt;&lt;div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Sempre que a planilha é atualizada, a função &lt;i&gt;INTERPRETAR()&lt;/i&gt; é executada, do mesmo modo que ocorre com qualquer fórmula ou operação. Com isso, é feita uma requisição à API, gerando um custo desnecessário.&lt;/li&gt;&lt;li&gt;Outro inconveniente, decorrente da atualização, é que a Data da transação, caso não seja informada na frase, é definida como a data corrente, conforme nossas instruções nos prompts. Dessa forma, depois de uma atualização, todas as transações passam a ter a data corrente e deixam de ter a data dos seus respectivos lançamentos.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;Devido a esses motivos, optamos por criar um menu personalizado e um item de menu que chama a função que fará a interpretação.&lt;/div&gt;&lt;h3 style=&quot;text-align: left;&quot;&gt;Menu personalizado&lt;/h3&gt;&lt;div&gt;Um menu personalizado é adicionado ao menu padrão do Google Sheets, ou de outra ferramenta do Google, utilizando a função &lt;i&gt;onOpen()&lt;/i&gt;, que define alguma ação que ocorre ao abrir uma planilha ou documento. Nessa função é criado o menu &lt;i&gt;IA Financeira&lt;/i&gt; e o item &lt;i&gt;Processar Frase Selecionada&lt;/i&gt;. O mesmo método que cria o item recebe o nome de uma função a ser executada, no caso&lt;i&gt; processarFraseIA&lt;/i&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; onOpen() {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  SpreadsheetApp.getUi()&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    .createMenu(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;IA Financeira&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    .addItem(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Processar Frase Selecionada&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;processarFraseIA&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    .addToUi();&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; processarFraseIA() {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; sheet &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; SpreadsheetApp.getActiveSheet();&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; cell &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; sheet.getActiveCell();&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; text &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; cell.getValue();&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;text &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;||&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;typeof&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; text &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;!==&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;string&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;) {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    SpreadsheetApp.getUi().alert(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Selecione uma célula com uma frase.&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  }&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; dados &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; interpretar(text);&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; row &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; cell.getRow();&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; col &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; cell.getColumn();&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;// Grava dados nas células seguintes&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  sheet.getRange(row, col &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;).setValue(dados.data);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  sheet.getRange(row, col &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;).setValue(dados.valor);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  sheet.getRange(row, col &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;).setValue(dados.categoria);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  sheet.getRange(row, col &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;).setValue(dados.tipo);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  SpreadsheetApp.getUi().alert(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Transação registrada!&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; interpretar(text) {&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; system_prompt &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Você é um sistema de categorização de transações financeiras. Responda APENAS em formato JSON válido, sem explicações adicionais.&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; user_prompt &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;`Extraia da frase abaixo:&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                        - valor (float, usar . como separador decimal)&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                        - tipo: &quot;Receita&quot; ou &quot;Despesa&quot;&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                        - categoria (uma palavra)&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                        - data (DD/MM/YYYY; se não informado,&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                           usar ${&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;Date&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;})&lt;/span&gt;

&lt;span style=&quot;color: #bd93f9;&quot;&gt;                    Frase: ${&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: #bd93f9;&quot;&gt;                    Retorne APENAS JSON, exemplo:&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                        {{&quot;valor&quot;: 58,.0, &quot;tipo&quot;: &quot;Despesa&quot;,&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                            &quot;categoria&quot;: &quot;Alimentacao&quot;,&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                            &quot;data&quot;: &quot;28/11/2025&quot;}}`&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  json_response &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;JSON&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;.parse(Utils.getResponseAI(system_prompt, user_prompt));&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; json_response;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;A função &lt;i&gt;processarFraseIA()&lt;/i&gt; espera que a célula selecionada contenha a frase a ser analisada. Então ela verifica a existência da frase e chama a função &lt;i&gt;interpretar()&lt;/i&gt;. Esta irá retornar um JSON, cujos dados são lançados nas células seguintes à direita da célula corrente.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Assim, você deve passar a ter um novo menu na sua planilha, como mostra a imagem:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;border: 1px solid rgb(0, 0, 0); text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3nDpLpdvKE-vq9qHXLcWTzSgXwJVFbBij3xgzDEAdEj6ifMlQeTdPrLai5KRRXyHMLBOYkyrB5S6dVNSUfGrl0UNogKWbOQCDiKUXbqfTsMH6CcXeYxyGoQGLGZPkqDJFrQoDO0l-uLwTjC6l8TD05xt2-i08uwSbxAgPcsxsKGS3fojngWHW/s1148/Captura%20de%20tela%20de%202025-12-12%2018-47-25.png&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;727&quot; data-original-width=&quot;1148&quot; height=&quot;406&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3nDpLpdvKE-vq9qHXLcWTzSgXwJVFbBij3xgzDEAdEj6ifMlQeTdPrLai5KRRXyHMLBOYkyrB5S6dVNSUfGrl0UNogKWbOQCDiKUXbqfTsMH6CcXeYxyGoQGLGZPkqDJFrQoDO0l-uLwTjC6l8TD05xt2-i08uwSbxAgPcsxsKGS3fojngWHW/w640-h406/Captura%20de%20tela%20de%202025-12-12%2018-47-25.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Tela da planilha com o menu personalizado&lt;br /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;div&gt;Agora, digite a frase na coluna A, por exemplo, e clique no menu &lt;i&gt;IA Financeira -&amp;gt; Processar Frase Selecionada&lt;/i&gt;. Os dados extraídos serão inseridos nas células à direita.&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Conclusão&lt;/h2&gt;&lt;div&gt;Apps Script nos oferece inúmeras possibilidades de criar planilhas poderosas para ajudar nosso controle das finanças pessoais. Mas deve ser usado com cautela, principalmente devido a questões relacionadas à segurança.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Apps Script pede permissão para acessar seus dados de Google Sheets, Google Drive, etc. Conceda apenas a permissão mínima necessária.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Evite copiar códigos de terceiros na Web. Eles podem fazer acesso aos seus dados, arquivos. Confie apenas em fontes verificadas.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Não exponha senhas, chaves de API, etc. no seu código. Procure guardá-los de forma segura, preferencialmente criptografados.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://professorcarlos.blogspot.com/feeds/1143860538802948421/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/33774862/1143860538802948421?isPopup=true' title='0 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/1143860538802948421'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/1143860538802948421'/><link rel='alternate' type='text/html' href='http://professorcarlos.blogspot.com/2025/12/como-integrar-chatgpt-com-google-sheets-usando-google-apps-script.html' title='Como Integrar ChatGPT com Google Sheets usando Google Apps Script'/><author><name>Professor Carlos</name><uri>http://www.blogger.com/profile/10347668022241306511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFBZD8558AE67R-CINkytW3INbUINaAhDuZ7GKm_4DSSKeHSpdm3K-TPQrhNPn5B-21wN24ZH8flimqC7ikTzmRrYZBAsyD_JSC8YW8BDJZwmTby9R7LZC9SYkyHkSs7NPVZxXiqEDqjhkRJNxZwqqyZaVgYjISiFpzXsSyGZmL1YFCQ/s220/WhatsApp%20Image%202024-05-17%20at%2017.42.05.jpeg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVsSQMBHNgt6QAtYQOtVL-1RCrj3wX-aTP4pVW6fRNvdpCyCG2rZZ727HN3-TXI9kNHVhGAqmus-SS1dUpbtOcepzucSi1roWzu5oj6J49XtvuAt-rvCm_TXhRG5FYnMwFdg-CfliuQGVwDUioiiUcFZu6s11UEAGI4OBZnzeaZVxZXTBHG5EX/s72-w640-h454-c/Untitled%20design%20(7)%20(1).png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-33774862.post-6872746559066562950</id><published>2025-12-09T18:34:00.003-03:00</published><updated>2025-12-09T20:37:44.744-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="automação"/><category scheme="http://www.blogger.com/atom/ns#" term="google sheets"/><category scheme="http://www.blogger.com/atom/ns#" term="IA"/><category scheme="http://www.blogger.com/atom/ns#" term="inteligência artificial"/><title type='text'>ChatGPT + Google Sheets: a combinação perfeita para controlar seus gastos automaticamente</title><content type='html'>&lt;h1 style=&quot;text-align: left;&quot;&gt;ChatGPT + Google Sheets: a combinação perfeita para controlar seus gastos automaticamente&lt;/h1&gt;&lt;div&gt;Controlar seus gastos no dia a dia nem sempre é uma tarefa simples. Em posts anteriores nós aprendemos &lt;a href=&quot;https://professorcarlos.blogspot.com/2025/11/como-automatizar-suas-financas-pessoais.html&quot; target=&quot;_blank&quot;&gt;como integrar o Python ao Google Sheets&lt;/a&gt;, e até vimos &lt;a href=&quot;https://professorcarlos.blogspot.com/2025/11/como-criar-um-bot-telegram-para-atualizar-planilha-financeira-Google-Sheets.html&quot; target=&quot;_blank&quot;&gt;como criar um bot Telegram que interage com uma planilha&lt;/a&gt; via mensagens e comandos. Nessas estratégias, o usuário quase não precisa manipular a planilha diretamente. Mas combinar ChatGPT com Google Sheets pode tornar tudo automático, rápido e bem mais intuitivo.&lt;/div&gt;&lt;div&gt;&amp;nbsp;&lt;/div&gt;&lt;div&gt;Neste post, você vai aprender passo a passo como montar um sistema de controle financeiro automático usando essas duas ferramentas. Essa integração se dá pela utilização de extensões do Google Sheets ou scripts, criados com Apps Script, que se conectam à API da OpenAI.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Apps Script é uma plataforma Javascript baseada em nuvem que permite a integração e automação de tarefas nos produtos do Google, e que requer escrever código. Já as extensões adicionam novas fórmulas ao conjunto existente do Google Sheets. Essas fórmulas, tais como &lt;i&gt;=GPT()&lt;/i&gt;, por exemplo, possibilitam o envio de prompts a um modelo selecionado.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhmVn6mHAnQFF2tZQlsll6J3b439ZEv8mt8s2mIz_tGVzU2imp9FWYNDHnKfYexsOypwCZROLBY6D7ItR-9zPq1YmBjxXRUvoAyhYkWi8p_7BeUeCsrcRvKdyOpv_Io0dPkKgGFtlMO4mIcViXVUwFE-htYEmhjD1attIzy5OBwhfOCHIfW4XUX/s6092/closeup-de-maos-usando-computador-laptop-com-tela-mostrando-analise-dados.jpg&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;4128&quot; data-original-width=&quot;6092&quot; height=&quot;434&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhmVn6mHAnQFF2tZQlsll6J3b439ZEv8mt8s2mIz_tGVzU2imp9FWYNDHnKfYexsOypwCZROLBY6D7ItR-9zPq1YmBjxXRUvoAyhYkWi8p_7BeUeCsrcRvKdyOpv_Io0dPkKgGFtlMO4mIcViXVUwFE-htYEmhjD1attIzy5OBwhfOCHIfW4XUX/w640-h434/closeup-de-maos-usando-computador-laptop-com-tela-mostrando-analise-dados.jpg&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Fonte:&amp;nbsp;https://br.freepik.com/&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Benefícios de usar ChatGPT + Google Sheets&lt;/h2&gt;&lt;div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;O ChatGPT permite interpretar linguagem natural — ou seja, você pode “conversar” com sua planilha, pedir para ela classificar despesas por categoria (alimentação, transporte, lazer etc.), corrigir fórmulas ou gerar resumos e insights.&lt;/li&gt;&lt;li&gt;O Google Sheets oferece flexibilidade, armazenamento na nuvem e liberdade para customizar colunas, categorias, relatórios mensais.&lt;/li&gt;&lt;li&gt;Escolhendo a integração certa, você reduz drasticamente o trabalho manual de digitar cada transação financeira, categorizar e conferir totais.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Extensões que podem ser usadas&lt;/h2&gt;&lt;div&gt;A fim de usar o ChatGPT integrado ao Google Sheets ou criar automações com ele, podemos citar algumas opções:&lt;/div&gt;&lt;div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;GPT for Sheets and Docs possibilita selecionar dentre algumas IAs (ChatGPT, Claude, Gemini, entre outras). Funciona tanto no Sheets quando no Docs e é voltado para operações em grande volume de dados. Período de teste gratuito.&lt;/li&gt;&lt;li&gt;ChatGPT para Google Slides, Sheets e Docs é baseado em OpenAI e Gemini. Uso gratuito com alguns recursos premium&amp;nbsp; pagos.&lt;/li&gt;&lt;li&gt;Easy GPT for Sheets: limitado apenas a GPT e gratuito. É necessário inserir o token da API OpenAI, ou seja, a extensão irá consumir seus créditos. Não é atualizada desde setembro de 2024. Mas pode ser uma opção para quem quer entrar nesse universo e experimentar o recurso. Depois pode escolher uma outra extensão ou até mesmo se aventurar no Apps Script.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div&gt;Existem muitas outras alternativas, inclusive você pode usar o Gemini. Há um botão no canto superior direito quando você abre uma planilha denominado &lt;i&gt;Teste o Gemini&lt;/i&gt;, que lhe dá acesso à ferramemta. Período de teste gratuito.&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Exemplo prático: montando sua planilha&lt;/h2&gt;&lt;div&gt;Aqui vai um passo a passo, considerando que você quer registrar suas despesas mensais, obter um relatório e eventualmente pedir à IA que faça algum diagnóstico e lhe dê insights sobre sua situação financeira.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;1. Instale a extensão de sua preferência&lt;/b&gt;. Para este exemplo nós instalamos a Easy GPT for Sheets. Com Google Sheets aberto no navegador, clique no menu&amp;nbsp;&lt;i&gt;Extensões -&amp;gt; Complementos -&amp;gt; Instalar complementos&lt;/i&gt;. Na janela que será aberta, digite na caixa &lt;i&gt;Pesquisar aplicativos&lt;/i&gt;: Easy GPT for Sheets. Diversas opções de aplicativos serão exibidas e você clica no primeiro &lt;i&gt;card&lt;/i&gt;. A página da extensão será exibida e você clica em &lt;i&gt;Instalar&lt;/i&gt;. Após a instalação, você clica de novo no menu &lt;i&gt;Extensões&lt;/i&gt; e &lt;i&gt;Easy GPT for Sheets&lt;/i&gt; será um dos itens do menu. Clique então na opção &lt;i&gt;Configure&lt;/i&gt; e informe o seu token da API OpenAI. Role a tela até encontrar o botão &lt;i&gt;Save Configuration&lt;/i&gt; e clique nele. Note que existe um botão para habilitar/desabilitar a extensão:&amp;nbsp;&lt;i&gt;Activate/Deactivate Add-on&lt;/i&gt;. Isso pode ser importante para não consumir seus créditos durante a criação da planilha.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;2. Crie sua planilha&lt;/b&gt;, com colunas como: Data, Descrição, Valor, Categoria, Tipo. Vamos denominar essa planilha como &lt;b&gt;Transações&lt;/b&gt;, por exemplo.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;3. Crie fórmulas de IA para registrar a transação automaticamente&lt;/b&gt;. Vamos digitar uma transação na coluna Descrição (B2), por exemplo: &quot;Paguei 50 reais no almoço&quot;. E vamos criar fórmulas de IA para extrair os dados, como Data, Valor, Categoria e Tipo. Na coluna Categoria (C2) digite:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;i&gt;=&lt;b&gt;GPT&lt;/b&gt;(&quot;Você é um sistema de categorização de transações financeiras. Classifique a despesa descrita em: &#39;&quot; &amp;amp; $B2 &amp;amp; &quot;&#39; Classifique apenas como Alimentacao, Remedios, Lazer, Limpeza, Outros&quot;)&lt;/i&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Assim, o ChatGPT irá extrair da nossa descrição uma categoria que se enquadre numa das opções fornecidas no prompt da fórmula. Você pode relacionar mais categorias, de acordo com sua necessidade. Em seguida copie essa fórmula para as demais células da coluna B.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Na coluna Data (A2) digite:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;i&gt;=&lt;b&gt;GPT&lt;/b&gt;(&quot;Você é um sistema de interpretação e categorização de transações financeiras. Extraia a data da frase: &#39;&quot; &amp;amp; $B2 &amp;amp; &quot;&#39;. Se não houver nenhuma data, retorne exatamente este texto: &#39;&quot; &amp;amp; &lt;b&gt;TEXTO&lt;/b&gt;(&lt;b&gt;HOJE()&lt;/b&gt;; &quot;dd/mm/yyyy&quot;) &amp;amp; &quot;&#39;. Não invente data e não altere o formato.&quot; )&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Este prompt pede à IA que retorne a data, se houver na descrição. Ou retorne a data corrente, caso contrário.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Na coluna Valor (D2) digite:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;i&gt;=&lt;b&gt;VALOR&lt;/b&gt;(&lt;b&gt;GPT&lt;/b&gt;(&quot;Você é um sistema de interpretação e categorização de transações financeiras. Extraia da frase: &#39;&quot; &amp;amp; $B2 &amp;amp; &quot;&#39; o valor float. Use , como separador decimal&quot;))&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Finalmente, na coluna Tipo (E2) digite:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;=&lt;b&gt;GPT&lt;/b&gt;(&quot;Você é um sistema de interpretação e categorização de transações financeiras. Classifique a transação descrita em: &#39;&quot; &amp;amp; $B2 &amp;amp; &quot;&#39; Classifique apenas como Receita ou Despesa&quot;)&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Estando essas fórmulas replicadas nas demais linhas e colunas, nós só precisamos digitar a descrição da transação na célula correspondente da coluna B. Você pode experimentar formatos variados de descrição para ver a IA em ação.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;4. Crie a aba Dashboard&lt;/b&gt;. Use fórmulas do Google Sheets para totalizar gastos por categorias, gerar gráficos etc. Por exemplo: Na célula A2 desta aba digite a fórmula que totaliza gastos por categoria:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;i&gt;=&lt;b&gt;QUERY&lt;/b&gt;(&#39;Transações&#39;!C3:D24; &quot;SELECT C, SUM(D) GROUP BY C LABEL SUM(D) &#39;Total por Categoria&#39;&quot;; 1)&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Estamos considerando que a última linha da tabela Transações é a 24.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;b&gt;5. Use o ChatGPT para analisar seus gastos e gerar insights&lt;/b&gt;. Por exemplo, na aba &lt;i&gt;Dashboard&lt;/i&gt;, escolha uma célula livre e digite:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;i&gt;=&lt;b&gt;GPT&lt;/b&gt;(&quot;Aqui estão as transações do mês (colunas separadas por -, linhas por ponto e vírgula): &#39;&quot; &amp;amp; &lt;b&gt;JOIN&lt;/b&gt;(&quot;;&quot;; &lt;b&gt;ARRAYFORMULA&lt;/b&gt;(&#39;Transações&#39;!A2:A24 &amp;amp; &quot;-&quot; &amp;amp; &#39;Transações&#39;!B2:B24 &amp;amp; &quot;-&quot; &amp;amp; &#39;Transações&#39;!C2:C24 &amp;amp; &quot;-&quot; &amp;amp; &#39;Transações&#39;!D2:D24 &amp;amp; &quot;-&quot; &amp;amp; &#39;Transações&#39;!E2:E24)) &amp;amp; &quot;&#39;. Analise os 3 maiores gastos e recomende ajustes.&quot;)&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Nesse prompt nós criamos uma string formada pelas linhas de &lt;i&gt;Transações&lt;/i&gt; utilizando &lt;i&gt;ARRAYFORMULA&lt;/i&gt;, que cria uma string com o valor de cada célula separado por um delimitador. No caso usamos - (hifen) visto que os valores numéricos usam vírgula como separador decimal. Esse resultado é passado para &lt;i&gt;JOIN&lt;/i&gt; criar nova string juntando todas as linhas, agora separadas por ponto e vírgula. Esses dados nós enviamos no prompt, esclarecendo ao ChatGPT quais são os delimitadores, e pedimos ao modelo uma análise.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Incremente o Dashboard com outros resumos e gráficos. Adeque às suas necessidades.&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Conclusão&lt;/h2&gt;&lt;div&gt;Adotando essa estratégia de controle de suas finanças:&lt;/div&gt;&lt;div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Você ganha clareza real sobre seus gastos — por categoria, por mês, com visualizações e resumos automáticos.&lt;/li&gt;&lt;li&gt;Reduz o esforço e minimiza a chance de erro na hora de classificar e organizar despesas.&lt;/li&gt;&lt;li&gt;Com a automação, você cria um hábito financeiro mais consistente — essencial para quem luta para manter o controle no dia a dia.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://professorcarlos.blogspot.com/feeds/6872746559066562950/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/33774862/6872746559066562950?isPopup=true' title='0 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/6872746559066562950'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/6872746559066562950'/><link rel='alternate' type='text/html' href='http://professorcarlos.blogspot.com/2025/12/chatgpt-google-sheets-combinacao-perfeita-para-controlar-seus-gastos-automaticamente.html' title='ChatGPT + Google Sheets: a combinação perfeita para controlar seus gastos automaticamente'/><author><name>Professor Carlos</name><uri>http://www.blogger.com/profile/10347668022241306511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFBZD8558AE67R-CINkytW3INbUINaAhDuZ7GKm_4DSSKeHSpdm3K-TPQrhNPn5B-21wN24ZH8flimqC7ikTzmRrYZBAsyD_JSC8YW8BDJZwmTby9R7LZC9SYkyHkSs7NPVZxXiqEDqjhkRJNxZwqqyZaVgYjISiFpzXsSyGZmL1YFCQ/s220/WhatsApp%20Image%202024-05-17%20at%2017.42.05.jpeg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhmVn6mHAnQFF2tZQlsll6J3b439ZEv8mt8s2mIz_tGVzU2imp9FWYNDHnKfYexsOypwCZROLBY6D7ItR-9zPq1YmBjxXRUvoAyhYkWi8p_7BeUeCsrcRvKdyOpv_Io0dPkKgGFtlMO4mIcViXVUwFE-htYEmhjD1attIzy5OBwhfOCHIfW4XUX/s72-w640-h434-c/closeup-de-maos-usando-computador-laptop-com-tela-mostrando-analise-dados.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-33774862.post-3552167581572886659</id><published>2025-12-02T15:41:00.006-03:00</published><updated>2026-02-08T08:41:28.552-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="assistente financeiro pessoal"/><category scheme="http://www.blogger.com/atom/ns#" term="automação"/><category scheme="http://www.blogger.com/atom/ns#" term="bot telegram"/><category scheme="http://www.blogger.com/atom/ns#" term="IA"/><category scheme="http://www.blogger.com/atom/ns#" term="inteligência artificial"/><title type='text'>Melhorando seu Assistente Financeiro Pessoal: Registre Despesas usando IA</title><content type='html'>&lt;h1 style=&quot;text-align: left;&quot;&gt;Melhorando Seu Assistente Financeiro Pessoal: Registre Despesas usando IA&lt;/h1&gt;
  &lt;p class=&quot;post-meta&quot;&gt;
    Tempo de leitura: 7 minutos
  &lt;/p&gt;

&lt;div&gt;Criamos um Bot Telegram com o objetivo de ser nosso assistente financeiro pessoal nessa tarefa de gerenciar nosso dinheiro. Até então, ele aceita comandos para registrar despesas e receitas, listar os últimos registros e até fazer um diagnóstico e nos oferecer insights para melhorar a gestão do nosso dinheiro. Se você não acompanhou, pode voltar aos posts:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;a href=&quot;https://professorcarlos.blogspot.com/2025/11/como-criar-um-bot-telegram-para-atualizar-planilha-financeira-Google-Sheets.html&quot; target=&quot;_blank&quot;&gt;Como criar um bot no Telegram para atualizar uma planilha no Google Sheets&lt;/a&gt; e&lt;/div&gt;&lt;div&gt;&lt;a href=&quot;https://professorcarlos.blogspot.com/2025/11/seu-assistente-financeiro-pessoal-no-telegram-integrando-ia-ao-bot-do-google-sheets.html&quot; target=&quot;_blank&quot;&gt;Como integrar IA ao Bot para atuar como um assistente financeiro&lt;/a&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Neste artigo vamos implementar uma funcionalidade que vai facilitar ainda mais nossa tarefa. Vamos enviar uma mensagem informando o valor da transação e a que se refere esse valor. A Inteligência Artificial irá então analisar a mensagem e inferir uma transação a ser lançada na planilha. Dessa maneira não se faz necessário fazermos um registro de transação estruturada como exige o comando &lt;i&gt;/save&lt;/i&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNqwZZRXFxNB4AxAPaBlqH6zSS7JIG9mCn_P6J0FF7-m49_lRB8cPvbKrMnO0YQMcr_6eZZffwYXwqhAyNkIxfE-damRaOgP7_po290B1-_Woy6s5SD2JoCFtWn2e4H-EuOmyjE2MGaeEZJalGEZKTSuxiIHhRaUSUikTZUe5O75oHJZHMzEfm/s6698/mulher-sorridente-no-escritorio-usando-laptop-e-falando-em-smartphone.jpg&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;4470&quot; data-original-width=&quot;6698&quot; height=&quot;428&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNqwZZRXFxNB4AxAPaBlqH6zSS7JIG9mCn_P6J0FF7-m49_lRB8cPvbKrMnO0YQMcr_6eZZffwYXwqhAyNkIxfE-damRaOgP7_po290B1-_Woy6s5SD2JoCFtWn2e4H-EuOmyjE2MGaeEZJalGEZKTSuxiIHhRaUSUikTZUe5O75oHJZHMzEfm/w640-h428/mulher-sorridente-no-escritorio-usando-laptop-e-falando-em-smartphone.jpg&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Fonte:&amp;nbsp;https://br.freepik.com/&lt;h2 style=&quot;text-align: left;&quot;&gt;Pré-requisitos&lt;/h2&gt;&lt;div style=&quot;text-align: left;&quot;&gt;Neste artigo iremos adicionar mais uma função de automação financeira ao nosso Bot, a qual usa IA para ler uma mensagem, de onde irá extrair os dados de uma transação e adicionar em nossa planilha de finanças pessoais.&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;Para isso, será necessário que você tenha lido os posts anteriores e, preferencialmente, clonado o repositório GitHub onde está o código do Bot.&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;Não será necessário instalar nenhuma nova biblioteca, pois iremos apenas precisar de &lt;i&gt;json&lt;/i&gt;, que é embutida no Python e uma nova classe do pacote &lt;i&gt;telegram&lt;/i&gt;, a &lt;i&gt;MessageHandler&lt;/i&gt;. No mais, tudo que já instalamos até o momento será usado.&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;O que vamos criar&lt;/h2&gt;&lt;div style=&quot;text-align: left;&quot;&gt;Até então, nosso assistente financeiro responde a comandos previamente programados: &lt;i&gt;start, save, help, list&lt;/i&gt; e &lt;i&gt;diagnostic&lt;/i&gt;. Agora queremos que a interação seja mais natural, sem necessidade de enviar mensagens estruturadas. Ou seja, queremos enviar uma mensagem do tipo &quot;gastei 80 no almoço&quot;, &quot;almoço 80&quot;, e a IA irá extrair dessa mensagem um registro que será efetivado na planilha financeira do Google Sheets.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;A nova função&lt;/h2&gt;&lt;div&gt;No código do Bot apresentado ao final &lt;a href=&quot;https://professorcarlos.blogspot.com/2025/11/seu-assistente-financeiro-pessoal-no-telegram-integrando-ia-ao-bot-do-google-sheets.html&quot; target=&quot;_blank&quot;&gt;deste post&lt;/a&gt;, acrescente as novas linhas conforme mostrado a seguir:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; dotenv &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;load_dotenv&lt;/span&gt;
&lt;span style=&quot;color: #6272a4;&quot;&gt;# Acrescentar este import&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; json&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; os&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; logging&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; pandas &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; pd&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; datetime &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;date&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; utils &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;conectar_google_sheets,&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;normalizar_string,&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;validar_chat_id)&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; telegram &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;Update&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; telegram.ext &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;ApplicationBuilder,&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;CommandHandler,&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;ContextTypes,&lt;/span&gt;
&lt;span style=&quot;color: #6272a4;&quot;&gt;# Acrescentar este import&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;MessageHandler,&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;filters)&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; openai &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;OpenAI&lt;/span&gt;

&lt;span style=&quot;color: #6272a4;&quot;&gt;# Restante do código igual ao do post anterior&lt;/span&gt;

&lt;span style=&quot;color: #6272a4;&quot;&gt;# Acrescentar esta função&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;async&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;interpretar&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(update,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;context):&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;mensagem&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;update&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;message&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;text&lt;/span&gt;

    &lt;span style=&quot;color: #6272a4;&quot;&gt;# Chama IA para extrair informação&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;try&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;resposta&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;client&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;chat&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;completions&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;create(&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;model&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;gpt-3.5-turbo&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;messages&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;[&lt;/span&gt;
                &lt;span style=&quot;color: #f8f8f2;&quot;&gt;{&lt;/span&gt;
                    &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;system&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
                    &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;&quot;&quot;Você é um sistema de categorização de&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                        transações financeiras. Responda APENAS em formato&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                        JSON válido, sem explicações adicionais.&quot;&quot;&quot;&lt;/span&gt;
                &lt;span style=&quot;color: #f8f8f2;&quot;&gt;},&lt;/span&gt;
                &lt;span style=&quot;color: #f8f8f2;&quot;&gt;{&lt;/span&gt;
                    &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;user&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
                    &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;&quot;&quot;Extraia da frase abaixo:&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                        - valor (float, usar . como separador decimal)&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                        - tipo: &quot;Receita&quot; ou &quot;Despesa&quot;&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                        - categoria (uma palavra)&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                        - data (DD/MM/YYYY; se não informado,&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                            usar {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;today()&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;strftime(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;%d/%m/%Y&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;})&lt;/span&gt;

&lt;span style=&quot;color: #bd93f9;&quot;&gt;                    Frase: &quot;{&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;mensagem&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&quot;&lt;/span&gt;

&lt;span style=&quot;color: #bd93f9;&quot;&gt;                    Retorne APENAS JSON, exemplo:&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                        {{&quot;valor&quot;: 58,.0, &quot;tipo&quot;: &quot;Despesa&quot;,&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                            &quot;categoria&quot;: &quot;Alimentacao&quot;,&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                            &quot;data&quot;: &quot;28/11/2025&quot;}}&quot;&quot;&quot;&lt;/span&gt;
                &lt;span style=&quot;color: #f8f8f2;&quot;&gt;}&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;],&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;max_tokens&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;200&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;temperature&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0.3&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;texto&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;resposta&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;choices[&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;message&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;content&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;strip()&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;except&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;Exception&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;e:&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;logger&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;exception(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Erro chamando OpenAI&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: #ff79c6;&quot;&gt;await&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;update&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;message&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;reply_text(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;Erro na API de IA: {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;

    &lt;span style=&quot;color: #6272a4;&quot;&gt;# Extrair JSON do texto&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;try&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;start&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;texto&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;find(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;{&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;end&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;texto&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;rfind(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;}&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #ffb86c;&quot;&gt;1&lt;/span&gt;
        &lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;start&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;or&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;end&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
            &lt;span style=&quot;color: #ff79c6;&quot;&gt;raise&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;ValueError(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Nenhum JSON encontrado na resposta&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;

        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;json_text&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;texto[start:end]&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;dados&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;json&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;loads(json_text)&lt;/span&gt;

        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;valor&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(dados[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;valor&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;])&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;tipo&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;dados[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;tipo&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;capitalize()&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;categoria&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;dados[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;categoria&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;capitalize()&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;data_str&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;dados&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;get(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;data&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;today()&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;isoformat())&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;strip()&lt;/span&gt;

        &lt;span style=&quot;color: #6272a4;&quot;&gt;# Validar e normalizar data&lt;/span&gt;
        &lt;span style=&quot;color: #ff79c6;&quot;&gt;try&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;data_obj&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;pd&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;to_datetime(&lt;/span&gt;
                    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;data_str,&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;format&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;%d/%m/%Y&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;errors&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;coerce&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
            &lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;pd&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;isna(data_obj):&lt;/span&gt;
                &lt;span style=&quot;color: #f8f8f2;&quot;&gt;data&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;today()&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;strftime(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;%d/%m/%Y&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
            &lt;span style=&quot;color: #ff79c6;&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
                &lt;span style=&quot;color: #f8f8f2;&quot;&gt;data&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;data_obj&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;strftime(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;%d/%m/%Y&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: #ff79c6;&quot;&gt;except&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;Exception&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;e:&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;logger&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;exception(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Erro ao parsear data: %s&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;e)&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;data&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;today()&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;strftime(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;%d/%m/%Y&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;

    &lt;span style=&quot;color: #ff79c6;&quot;&gt;except&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;Exception&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;e:&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;logger&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;exception(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Erro ao parsear resposta da IA&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: #ff79c6;&quot;&gt;await&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;update&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;message&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;reply_text(&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;Não consegui interpretar. Erro: {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}\nResposta: {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;texto[:&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;500&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&quot;&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;

    &lt;span style=&quot;color: #ff79c6;&quot;&gt;try&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;sheet&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;abrir_planilha()&lt;/span&gt;
        &lt;span style=&quot;color: #6272a4;&quot;&gt;# Adiciona uma nova linha na planilha com os dados extraídos&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;sheet&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;append_row(&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;[&lt;/span&gt;
                &lt;span style=&quot;color: #f8f8f2;&quot;&gt;data,&lt;/span&gt;
                &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
                &lt;span style=&quot;color: #f8f8f2;&quot;&gt;categoria,&lt;/span&gt;
                &lt;span style=&quot;color: #f8f8f2;&quot;&gt;valor,&lt;/span&gt;
                &lt;span style=&quot;color: #f8f8f2;&quot;&gt;tipo,&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: #ff79c6;&quot;&gt;await&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;update&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;message&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;reply_text(&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;📌 Registrado!\n\n&quot;&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;Tipo: {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;tipo&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}\n&quot;&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;Categoria: {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;categoria&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}\n&quot;&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;Valor: R$ {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;valor&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;:,.2f}\n&quot;&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;Data: {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&quot;&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;except&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;Exception&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;e:&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;logger&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;exception(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Erro ao salvar na planilha&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: #ff79c6;&quot;&gt;await&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;update&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;message&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;reply_text(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;Erro ao salvar na planilha: {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;E vamos então analisar a função &lt;i&gt;interpretar()&lt;/i&gt;, responsável por extrair os dados de uma mensagem enviada em linguagem natural.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Inicialmente, lemos a mensagem enviada ao Bot. Em seguida montamos um prompt para enviar essa mensagem à IA. Note o contexto no prompt e que pedimos quais dados devem ser extraídos. Ainda é solicitado que esses dados sejam retornados em formato &lt;i&gt;json&lt;/i&gt;. É importante observar que, mesmo que tenhamos pedido que a IA retorne apenas &lt;i&gt;json&lt;/i&gt;, não há garantia que a resposta será assim. Por isso precisamos tratar adequadamente a resposta.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Observe que podemos enviar uma mensagem com uma data passada, para o caso de termos esquecido de fazer um registro. Mas se não enviarmos a data na mensagem, pedimos à IA que considere o lançamento na data corrente.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;A fim de extrair o &lt;i&gt;json&lt;/i&gt; da resposta, primeiro buscamos as chaves {}, que envolvem um &lt;i&gt;json&lt;/i&gt;. Isso irá garantir que existe um &lt;i&gt;json&lt;/i&gt; no texto da resposta e que iremos tratar apenas ele. Daí lemos as chaves fundamentais do nosso registro, que são a &lt;i&gt;data&lt;/i&gt;, o &lt;i&gt;valor&lt;/i&gt;, a &lt;i&gt;categoria&lt;/i&gt; e o &lt;i&gt;tipo&lt;/i&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Lidos esses dados, fazemos o lançamento na planilha. Faça vários testes com a IA, enviando mensagens com dados em ordem diferente, data em vários formatos, a fim de confirmar se o modelo irá tratá-las adequadamente.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Finalmente, para adicionar a função ao bot, inclua a seguinte linha à sequencia de &lt;i&gt;app.add_handler()&lt;/i&gt; que já existem no método &lt;i&gt;main()&lt;/i&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;add_handler(&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;MessageHandler(authorized_only&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;filters&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;TEXT&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;~&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;filters&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;COMMAND,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;interpretar))&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Implementando autorização de uso&lt;/h2&gt;&lt;div&gt;Até então não abordamos o tema autorização, que trata de como o bot lida com o acesso de pessoas que não tem permissão para usar os recursos disponíveis. Isso é importante, visto que se trata de um bot de uso pessoal. Ele manipula uma planilha Google Sheets de uso pessoal, e, sendo o bot de acesso público, é fundamental tratar essa questão.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;No código do nosso bot nós utilizamos o módulo &lt;i&gt;filters&lt;/i&gt;, o qual permite que o bot decida qual mensagem ou comando ele deve tratar. Existem filtros nativos da biblioteca e filtros personalizados, que podem ser criados pelo desenvolvedor.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Neste caso, optamos por um filtro personalizado. Mas a biblioteca oferece &lt;i&gt;filters.User()&lt;/i&gt;, que permite ao bot tratar mensagens ou comandos de usuários especificados, tanto pelo ID quanto pelo &lt;i&gt;username&lt;/i&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;No filtro que fizemos, foi criada uma lista de IDs de usuários autorizados, a qual mantemos no arquivo &lt;i&gt;.env&lt;/i&gt;. Então, sempre que o usuário envia uma mensagem ao bot, o filtro executa e verifica de o ID do uauário pertence a essa lista. Veja como está implementado o filtro:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;CHAT_ID_LIST&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;ast&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;literal_eval(os&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;getenv(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;CHAT_ID_LIST&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;[]&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;))&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;OPENAI_API_KEY&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;os&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;getenv(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;OPENAI_API_KEY&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;


&lt;span style=&quot;color: #f8f8f2;&quot;&gt;client&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;OpenAI(api_key&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;OPENAI_API_KEY)&lt;/span&gt;


&lt;span style=&quot;color: #6272a4;&quot;&gt;# Filtro customizado para validação de chat_id&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;AuthorizedOnlyFilter&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(filters&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;MessageFilter):&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2; font-style: italic;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;update:&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;Update)&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;bool&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
        &lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;not&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;update&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;or&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;not&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;update&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;from_user:&lt;/span&gt;
            &lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;False&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;chat_id&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;update&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;from_user&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;id&lt;/span&gt;
        &lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;chat_id&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;CHAT_ID_LIST&lt;/span&gt;


&lt;span style=&quot;color: #6272a4;&quot;&gt;# Instanciar uma única vez&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;authorized_only&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;AuthorizedOnlyFilter()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;O filtro é criado como uma subclasse de &lt;i&gt;filters.MessageFilter&lt;/i&gt;&amp;nbsp;na sobrescrevemos o método &lt;i&gt;filter()&lt;/i&gt;. Nesse método, se o ID do usuário não pertencer à lista, ele retorna &lt;i&gt;False&lt;/i&gt;, e a mensagem não é tratada pelo bot.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Criada a classe, nós a instanciamos. E é essa instância que é passada como parâmetro na criação dos &lt;i&gt;handlers&lt;/i&gt; de comandos e mensagens no método &lt;i&gt;main()&lt;/i&gt;:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;add_handler(&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;CommandHandler(&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;start&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;start,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;filters&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;authorized_only))&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;add_handler(&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;CommandHandler(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;help&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;help_command,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;filters&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;authorized_only))&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;add_handler(&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;CommandHandler(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;save&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;save_command,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;filters&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;authorized_only))&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;add_handler(&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;CommandHandler(&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;last&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;print_last_transactions,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;filters&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;authorized_only))&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;add_handler(&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;CommandHandler(&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;diagnostic&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;diagnostic_command,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;filters&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;authorized_only))&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;add_handler(&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;MessageHandler(&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;authorized_only&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;filters&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;TEXT&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;~&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;filters&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;COMMAND,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;interpretar))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;Confira o código completo no &lt;a href=&quot;https://github.com/stonefullstm/nfinance-bot-ai&quot; target=&quot;_blank&quot;&gt;GitHub&lt;/a&gt;.&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Conclusão&lt;/h2&gt;&lt;div&gt;Agora o nosso assistente financeiro aceita registro de despesas por meio do comando &lt;i&gt;/save&lt;/i&gt;, usando dados estruturados, ou por meio de uma mensagem em linguagem natural, onde a IA interpreta a mensagem, infere os dados e efetiva o lançamento na planilha Google Sheets.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Essa funcionalidade pode ser melhorada, se definirmos um conjunto de categorias nas quais as depesas podem ser lançadas. Essa função vamos deixar para você implementar.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://professorcarlos.blogspot.com/feeds/3552167581572886659/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/33774862/3552167581572886659?isPopup=true' title='0 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/3552167581572886659'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/3552167581572886659'/><link rel='alternate' type='text/html' href='http://professorcarlos.blogspot.com/2025/12/melhorando-seu-assistente-financeiro-registre-despesas-usando-ia.html' title='Melhorando seu Assistente Financeiro Pessoal: Registre Despesas usando IA'/><author><name>Professor Carlos</name><uri>http://www.blogger.com/profile/10347668022241306511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFBZD8558AE67R-CINkytW3INbUINaAhDuZ7GKm_4DSSKeHSpdm3K-TPQrhNPn5B-21wN24ZH8flimqC7ikTzmRrYZBAsyD_JSC8YW8BDJZwmTby9R7LZC9SYkyHkSs7NPVZxXiqEDqjhkRJNxZwqqyZaVgYjISiFpzXsSyGZmL1YFCQ/s220/WhatsApp%20Image%202024-05-17%20at%2017.42.05.jpeg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNqwZZRXFxNB4AxAPaBlqH6zSS7JIG9mCn_P6J0FF7-m49_lRB8cPvbKrMnO0YQMcr_6eZZffwYXwqhAyNkIxfE-damRaOgP7_po290B1-_Woy6s5SD2JoCFtWn2e4H-EuOmyjE2MGaeEZJalGEZKTSuxiIHhRaUSUikTZUe5O75oHJZHMzEfm/s72-w640-h428-c/mulher-sorridente-no-escritorio-usando-laptop-e-falando-em-smartphone.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-33774862.post-34063633434141020</id><published>2025-11-24T22:00:00.014-03:00</published><updated>2025-11-28T09:08:39.390-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="automação"/><category scheme="http://www.blogger.com/atom/ns#" term="finanças pessoais"/><category scheme="http://www.blogger.com/atom/ns#" term="IA"/><category scheme="http://www.blogger.com/atom/ns#" term="inteligência artificial"/><title type='text'>5 erros que sabotam suas finanças mesmo que você ganhe bem</title><content type='html'>&lt;h1&gt;5 erros que sabotam suas finanças mesmo que você ganhe bem&lt;/h1&gt;

&lt;p data-end=&quot;527&quot; data-start=&quot;288&quot;&gt;Existem pessoas que ganham pouco e conseguem prosperar — e pessoas que ganham muito e vivem no aperto.&amp;nbsp;A diferença raramente está em quanto a pessoa ganha mensalmente.&amp;nbsp;Na verdade, pode estar mais nos &lt;strong data-end=&quot;456&quot; data-start=&quot;445&quot;&gt;hábitos&lt;/strong&gt;, nos &lt;strong data-end=&quot;474&quot; data-start=&quot;462&quot;&gt;sistemas&lt;/strong&gt; e na forma como lidamos com o dinheiro no dia a dia.&lt;/p&gt;&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVtAPu9GCwRV6_euJ8dgurtW9AJs-bbjtplcekZOHUor79y7RVvoYJ59V0lGJWo1eBmo4lJyKHx6XM311AYHprZnvwq5bPdK3QMJRrUMDWeHU6E1kXQHfg7AiQX75TZuN1-LCu9G4vuPMDcTiKcCEYWfICo19wYZa_2F0U5HHRIA0JsIzN1djA/s7952/beautiful-freelancer-stock-broker-signing-business-deal-her-work-desk-successful-businesswoman-reading-work-contract.jpg&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img alt=&quot;Pessoa trabalhando nas suas finanças&quot; border=&quot;0&quot; data-original-height=&quot;5304&quot; data-original-width=&quot;7952&quot; height=&quot;426&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVtAPu9GCwRV6_euJ8dgurtW9AJs-bbjtplcekZOHUor79y7RVvoYJ59V0lGJWo1eBmo4lJyKHx6XM311AYHprZnvwq5bPdK3QMJRrUMDWeHU6E1kXQHfg7AiQX75TZuN1-LCu9G4vuPMDcTiKcCEYWfICo19wYZa_2F0U5HHRIA0JsIzN1djA/w640-h426/beautiful-freelancer-stock-broker-signing-business-deal-her-work-desk-successful-businesswoman-reading-work-contract.jpg&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Fonte: https://br.freepik.com/&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p data-end=&quot;687&quot; data-start=&quot;529&quot;&gt;Se você já se perguntou por que parece que o dinheiro “escorre entre os dedos”, mesmo tendo um bom trabalho e tendo uma renda acima da média, este post é para você.&lt;/p&gt;
&lt;p data-end=&quot;864&quot; data-start=&quot;689&quot;&gt;A seguir, você vai ver &lt;strong data-end=&quot;738&quot; data-start=&quot;712&quot;&gt;os 5 erros silenciosos&lt;/strong&gt; que sabotam suas finanças sem que você perceba — e como corrigi-los usando &lt;em data-end=&quot;863&quot; data-start=&quot;814&quot;&gt;automação, organização&lt;/em&gt;&lt;span data-end=&quot;863&quot; data-start=&quot;814&quot;&gt; e um &lt;/span&gt;&lt;span data-end=&quot;863&quot; data-start=&quot;814&quot;&gt;pouco de &lt;i&gt;tecnologia&lt;/i&gt;&lt;/span&gt;.&lt;/p&gt;
&lt;hr data-end=&quot;869&quot; data-start=&quot;866&quot; /&gt;
&lt;h2 data-end=&quot;928&quot; data-start=&quot;871&quot;&gt;&lt;strong data-end=&quot;928&quot; data-start=&quot;874&quot;&gt;1. Falta de clareza sobre para onde o dinheiro vai&lt;/strong&gt;&lt;/h2&gt;
&lt;p data-end=&quot;1076&quot; data-start=&quot;930&quot;&gt;Esse é o erro número 1 pela mesma razão que navegadores marítimos antigos temiam navegar sem mapa:&amp;nbsp;&lt;strong data-end=&quot;1076&quot; data-start=&quot;1021&quot;&gt;se você não sabe onde está, não sabe para onde vai.&lt;/strong&gt;&lt;/p&gt;
&lt;p data-end=&quot;1171&quot; data-start=&quot;1078&quot;&gt;A maioria das pessoas não tem problema de renda. O problema reside em não fazer adequadamente um orçamento, ou não fazer orçamento algum. Gastar o dinheiro sem rastrear sua renda e despesas pode levar a gastos excessivos e acumulação de dívidas.&lt;/p&gt;&lt;p data-end=&quot;1205&quot; data-start=&quot;1173&quot;&gt;E muitas vezes o cérebro cria uma armadilha. Você pensa; “Ah, eu sei mais ou menos o quanto eu gasto…”. Só que o “mais ou menos” sempre mascara vazamentos significativos, como aquelas assinaturas esquecidas, gastos pequenos e frequentes com delivery ou o aumento gradual do padrão de vida.&lt;/p&gt;
&lt;h3 data-end=&quot;1318&quot; data-start=&quot;1297&quot;&gt;&lt;strong data-end=&quot;1318&quot; data-start=&quot;1301&quot;&gt;Como corrigir&lt;/strong&gt;&lt;/h3&gt;
&lt;ul data-end=&quot;1654&quot; data-start=&quot;1319&quot;&gt;
&lt;li data-end=&quot;1492&quot; data-start=&quot;1319&quot;&gt;
&lt;p data-end=&quot;1398&quot; data-start=&quot;1321&quot;&gt;Crie um sistema automático de registro de gastos. Isso pode evitar erros e a &lt;b&gt;preguiça&lt;/b&gt; de fazer esses registros de modo manual.&amp;nbsp;Se quiser algo simples, sugerimos:&lt;/p&gt;
&lt;ul data-end=&quot;1492&quot; data-start=&quot;1401&quot;&gt;
&lt;li data-end=&quot;1432&quot; data-start=&quot;1401&quot;&gt;
&lt;p data-end=&quot;1432&quot; data-start=&quot;1403&quot;&gt;Google Sheets + Scripts em Python:&amp;nbsp;Crie uma planilha com categorias pré-definidas (Moradia, Alimentação, Transporte, Lazer, Investimentos, etc.). Use um script em Python para conectar seu e-mail (onde chegam os comprovantes) ou mesmo seu banco (via API, se disponível) para preencher automaticamente a planilha. Um script simples pode classificar gastos automaticamente com base em palavras-chave. Um ponto de partida pode ser visto no post&amp;nbsp;&lt;a href=&quot;https://professorcarlos.blogspot.com/2025/11/como-automatizar-suas-financas-pessoais.html&quot; target=&quot;_blank&quot;&gt;Como Automatizar suas Finanças Pessoais com Google Sheets e Python&lt;/a&gt;&amp;nbsp;&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;1492&quot; data-start=&quot;1435&quot;&gt;
&lt;p data-end=&quot;1492&quot; data-start=&quot;1437&quot;&gt;Um bot no Telegram (como &lt;a href=&quot;https://professorcarlos.blogspot.com/2025/11/como-criar-um-bot-telegram-para-atualizar-planilha-financeira-Google-Sheets.html&quot; target=&quot;_blank&quot;&gt;aquele que estamos criando&lt;/a&gt;), no qual você registar seus gastos com a simplicidade de enviar um mensagem a um contato, tais como &quot;30 comida&quot; ou &quot;150 transporte&quot;. O bot registra o gasto em uma planilha ou banco de dados em tempo real. É rápido, discreto e elimina a pilha de comprovantes.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li data-end=&quot;1577&quot; data-start=&quot;1493&quot;&gt;
&lt;p data-end=&quot;1577&quot; data-start=&quot;1495&quot;&gt;Use categorias fixas, que são frequentes do seu cotidiano: moradia, alimentação, transporte, lazer, investimentos, etc.&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;1654&quot; data-start=&quot;1578&quot;&gt;
&lt;p data-end=&quot;1654&quot; data-start=&quot;1580&quot;&gt;Evite rastrear suas despesas 100% manualmente. Quanto mais automático, mais sustentável.&lt;/p&gt;
&lt;/li&gt;&lt;/ul&gt;O ponto está em transformar o rastreamento de uma tarefa chata e manual em um processo passivo e contínuo. A visibilidade total é o primeiro passo para o controle.&lt;br /&gt;&lt;ul data-end=&quot;1654&quot; data-start=&quot;1319&quot;&gt;
&lt;/ul&gt;
&lt;hr data-end=&quot;1659&quot; data-start=&quot;1656&quot; /&gt;
&lt;h2 data-end=&quot;1700&quot; data-start=&quot;1661&quot;&gt;&lt;strong data-end=&quot;1700&quot; data-start=&quot;1664&quot;&gt;2. Não pagar a si mesmo primeiro&lt;/strong&gt;&lt;/h2&gt;
&lt;p data-end=&quot;1784&quot; data-start=&quot;1702&quot;&gt;Quanto sobra da sua renda no fim do mês?&amp;nbsp;Se a resposta é “quase nada”, você não está sozinho. O problema é que a lógica está invertida.&lt;/p&gt;
&lt;p data-end=&quot;1938&quot; data-start=&quot;1915&quot;&gt;A maioria faz assim: Recebe → Paga contas → Compra coisas → Se sobrar… investe. O ponto aqui é que nunca sobra.&lt;/p&gt;&lt;p data-end=&quot;1938&quot; data-start=&quot;1915&quot;&gt;A regra é simples e inegociável: Investimentos não são o que sobra. São o que vem primeiro, são a prioridade.&lt;/p&gt;
&lt;h3 data-end=&quot;1966&quot; data-start=&quot;1940&quot;&gt;&lt;strong data-end=&quot;2054&quot; data-start=&quot;2037&quot;&gt;Como corrigir&lt;/strong&gt;&lt;/h3&gt;
&lt;ul data-end=&quot;2260&quot; data-start=&quot;2055&quot;&gt;
&lt;li data-end=&quot;2133&quot; data-start=&quot;2055&quot;&gt;
&lt;p data-end=&quot;2133&quot; data-start=&quot;2057&quot;&gt;Configure um &lt;strong data-end=&quot;2088&quot; data-start=&quot;2070&quot;&gt;PIX automático&lt;/strong&gt; ou transferência agendada.&amp;nbsp;No dia mesmo (ou no dia seguinte) em que seu salário cair na conta, configure uma transferência automática para a sua corretora ou conta de investimentos. Isso não é uma sugestão, é uma lei pessoal. O valor some da sua conta corrente antes que você sequer pense em gastá-lo.&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;2198&quot; data-start=&quot;2134&quot;&gt;
&lt;p data-end=&quot;2198&quot; data-start=&quot;2136&quot;&gt;Algumas corretoras oferecem ferramentas com a função &quot;Round-up&quot;, que usa IA para arredondar seus gastos no cartão de crédito e investir o troco. Por exemplo, uma compra de R$ 19,70 vira R$ 20,00, e os R$ 0,30 são investidos automaticamente. É uma automação microscópica e poderosa.&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;2260&quot; data-start=&quot;2199&quot;&gt;
&lt;p data-end=&quot;2260&quot; data-start=&quot;2201&quot;&gt;Crie uma planilha que projeta o crescimento do seu patrimônio com base no valor mensal que você está investindo. Usar a função FV (Future Value) no Google Sheets para ver o impacto de R$ 500/mês em 20 anos, por exemplo, é um poderoso reforço motivacional. A automação do registro (conectada à sua corretera via API) torna isso vivo.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p data-end=&quot;2319&quot; data-start=&quot;2262&quot;&gt;Automação elimina o “querer” e substitui por “acontecer”, vai garantir que o investimento nunca atrase.&lt;/p&gt;
&lt;hr data-end=&quot;2324&quot; data-start=&quot;2321&quot; /&gt;
&lt;h2 data-end=&quot;2381&quot; data-start=&quot;2326&quot;&gt;&lt;strong data-end=&quot;2381&quot; data-start=&quot;2329&quot;&gt;3. Misturar gastos essenciais com estilo de vida&lt;/strong&gt;&lt;/h2&gt;
&lt;p data-end=&quot;2424&quot; data-start=&quot;2383&quot;&gt;Mesmo quem ganha bem cai nessa armadilha. O exemplo clássico é o &quot;Efeito Inflação do Estilo de Vida&quot;: seu salário aumenta, e imediatamente seu padrão de consumo acompanha. O aluguel aumenta, o plano de celular melhora, os restaurantes ficam mais caros. Você ganha mais, mas a sobra é a mesma (ou menor).&lt;/p&gt;
&lt;h3 data-end=&quot;2635&quot; data-start=&quot;2614&quot;&gt;&lt;strong data-end=&quot;2635&quot; data-start=&quot;2618&quot;&gt;Como corrigir&lt;/strong&gt;&lt;/h3&gt;&lt;ul data-end=&quot;1654&quot; data-start=&quot;1319&quot;&gt;&lt;li data-end=&quot;1492&quot; data-start=&quot;1319&quot;&gt;&lt;p data-end=&quot;1398&quot; data-start=&quot;1321&quot;&gt;Estratégia das Contas Bancárias Múltiplas. Esta é a forma mais prática de separação física:&lt;/p&gt;&lt;ul data-end=&quot;1492&quot; data-start=&quot;1401&quot;&gt;&lt;li data-end=&quot;1432&quot; data-start=&quot;1401&quot;&gt;&lt;p data-end=&quot;1432&quot; data-start=&quot;1403&quot;&gt;Conta 1 (Salário &amp;amp; Contas Fixas): Para onde seu salário cai. Todas as contas fixas (aluguel, luz, internet, escola) têm débito automático aqui.&lt;/p&gt;&lt;/li&gt;&lt;li data-end=&quot;1492&quot; data-start=&quot;1435&quot;&gt;&lt;p data-end=&quot;1492&quot; data-start=&quot;1437&quot;&gt;Conta 2 (Estilo de Vida): Para gastos variáveis (lazer, restaurantes, compras). No dia do salário, uma transferência automática de um valor predeterminado (ex: R$ 1.500) é feita da Conta 1 para a Conta 2. Quando o dinheiro acaba, acabou.&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li data-end=&quot;1577&quot; data-start=&quot;1493&quot;&gt;&lt;p data-end=&quot;1577&quot; data-start=&quot;1495&quot;&gt;Cartões de Crédito Diferentes. Tenha um cartão apenas para gastos essenciais e outro para lazer. A fatura de cada um debita de sua respectiva conta. Isso evita a sensação de que &quot;está sobrando&quot; na conta principal e permite um controle granular.&lt;/p&gt;&lt;/li&gt;&lt;li data-end=&quot;1654&quot; data-start=&quot;1578&quot;&gt;&lt;p data-end=&quot;1654&quot; data-start=&quot;1580&quot;&gt;Alertas em planilhas ou Apps de Controle: Configure alertas nos seus aplicativos de finanças para avisá-lo quando seus gastos com uma categoria (ex: &quot;Restaurantes&quot;) ultrapassarem, por exemplo, 80% do limite mensal estabelecido. &lt;a href=&quot;https://professorcarlos.blogspot.com/2025/11/como-enviar-notificacoes-por-e-mail-ou-pelo-telegram.html&quot; target=&quot;_blank&quot;&gt;Neste artigo&lt;/a&gt; mostramos como enviar alertas pelo Telegram ou e-mail.&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;Crie um &quot;orçamento de estilo de vida&quot; com um teto rígido e automático. Seu padrão de vida fica contido, permitindo que o aumento da renda vá para os investimentos, não para o consumo.&lt;br /&gt;
&lt;hr data-end=&quot;2990&quot; data-start=&quot;2987&quot; /&gt;
&lt;h2 data-end=&quot;3052&quot; data-start=&quot;2992&quot;&gt;&lt;strong data-end=&quot;3052&quot; data-start=&quot;2995&quot;&gt;4. Não ter um sistema de tomada de decisão financeira&lt;/strong&gt;&lt;/h2&gt;
&lt;p data-end=&quot;3099&quot; data-start=&quot;3054&quot;&gt;A maioria das pessoas decide financeiramente com base no humor do dia, promoções, impulsos ou na comparação com outras pessoas. O problema não está em gastar — o problema é gastar sem critério.&lt;/p&gt;
&lt;h3 data-end=&quot;3289&quot; data-start=&quot;3268&quot;&gt;&lt;strong data-end=&quot;3289&quot; data-start=&quot;3272&quot;&gt;Como corrigir&lt;/strong&gt;&lt;/h3&gt;
&lt;p data-end=&quot;3325&quot; data-start=&quot;3290&quot;&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Crie um &quot;Checklist de Decisão&quot;: Antes de qualquer compra não seja essencial, acima de um determinado valor, por exemplo R$ 200, responda a este questionário:&lt;/li&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Necessidade vs. Desejo: Isso é necessário agora ou pode esperar 30 dias? (A Regra dos 30 dias elimina 90% dos impulsos).&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Oportunidade: Há algo mais importante que esse dinheiro poderia resolver? (Ex: quitar uma dívida, completar a reserva de emergência).&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Alinhamento: Esse gasto me aproxima ou me afasta dos meus objetivos financeiros de curto, médio e longo prazo?&lt;/li&gt;&lt;br /&gt;&lt;/ul&gt;&lt;li&gt;Automatize o Registro da Decisão: Use seu bot do Telegram ou uma planilha com um formulário para registrar toda compra ponderada. Colunas: &quot;Item&quot;, &quot;Valor&quot;, &quot;Data da Decisão&quot;, &quot;Motivo&quot;, &quot;Arrependimento (Sim/Não)&quot;. Revisar esse histórico mensalmente é um aprendizado brutal sobre seus padrões de consumo.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Use Extensões de Comparação de Preços: Ferramentas como Zoom ou Buscapé oferecem extensões de navegador que, com um clique, usam IA para verificar o histórico de preços de um produto e alertar se é realmente uma promoção ou uma armadilha.&lt;/li&gt;&lt;/ul&gt;A proposta consiste em substituir a decisão emocional por um processo racional e documentado. A sistemática vence a impulsividade.&lt;br /&gt;&lt;p&gt;&lt;/p&gt;
&lt;hr data-end=&quot;3744&quot; data-start=&quot;3741&quot; /&gt;
&lt;h2 data-end=&quot;3804&quot; data-start=&quot;3746&quot;&gt;&lt;strong data-end=&quot;3804&quot; data-start=&quot;3749&quot;&gt;5. Depender da força de vontade (e não de sistemas)&lt;/strong&gt;&lt;/h2&gt;
&lt;p data-end=&quot;3831&quot; data-start=&quot;3806&quot;&gt;Esse é o erro mais cruel. As pessoas tentam controlar o dinheiro com força de vontade, disciplina e promessas de “agora vai”. Mas a neurociência é clara: a força de vontade é um recurso finito e cansável. Um sistema automático é robusto e não cansa.&lt;/p&gt;
&lt;h3 data-end=&quot;4011&quot; data-start=&quot;3990&quot;&gt;&lt;strong data-end=&quot;4011&quot; data-start=&quot;3994&quot;&gt;Como corrigir&lt;/strong&gt;&lt;/h3&gt;
&lt;p data-end=&quot;4044&quot; data-start=&quot;4012&quot;&gt;Implemente sistemas automáticos:&lt;/p&gt;
&lt;ul data-end=&quot;4295&quot; data-start=&quot;4045&quot;&gt;
&lt;li data-end=&quot;4087&quot; data-start=&quot;4045&quot;&gt;
&lt;p data-end=&quot;4087&quot; data-start=&quot;4047&quot;&gt;Sistema de Débitos Automáticos Totais. Automatize TUDO que for essencial e previsível: aluguel, condomínio, contas de consumo, internet, seguros, e o mais importante, os investimentos.&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;4132&quot; data-start=&quot;4088&quot;&gt;
&lt;p data-end=&quot;4132&quot; data-start=&quot;4090&quot;&gt;Painel de Controle em Tempo Real (Dashboard). Crie um painel numa planilha avançada que se atualize automaticamente (via importação de dados ou APIs). Esse painel deve mostrar: saldo das contas, gastos por categoria, progresso das metas de investimento, etc. A visão macro e automática tira o peso da microgestão.&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;4167&quot; data-start=&quot;4133&quot;&gt;&lt;p data-end=&quot;4167&quot; data-start=&quot;4135&quot;&gt;Alertas proativos com Apps Script/Python. Programe scripts simples que analisam sua planilha e disparam e-mails ou mensagens no Telegram quando detectam anomalias. Ex: &quot;Seu gasto com &#39;Lazer&#39; neste mês já é 50% maior que a média dos últimos 3 meses&quot; ou &quot;Sua conta corrente está abaixo do limite de segurança&quot;. A IA, neste caso, pode atuar como seu assistente financeiro pessoal.&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;
A ideia é automatizar sua gestão financeira. Você não precisa ser disciplinado todos os dias; você só precisa ter disciplina uma vez para montar o sistema. Depois, o sistema trabalha para você. Claro, precisa seguir aprimorando sempre.&lt;br /&gt;&lt;hr data-end=&quot;4375&quot; data-start=&quot;4372&quot; /&gt;
&lt;h1 data-end=&quot;4392&quot; data-start=&quot;4377&quot;&gt;&lt;strong data-end=&quot;4392&quot; data-start=&quot;4379&quot;&gt;Conclusão&lt;/strong&gt;&lt;/h1&gt;
&lt;p data-end=&quot;4448&quot; data-start=&quot;4394&quot;&gt;Ganhar bem ajuda — claro que ajuda. Mas não resolve.&lt;/p&gt;&lt;p data-end=&quot;4448&quot; data-start=&quot;4394&quot;&gt;O que resolve é ter sistemas, clareza e hábitos financeiros inteligentes. A boa notícia é que a tecnologia está do seu lado. Ferramentas que antes eram restritas a grandes empresas — como automação, análise de dados e IA — agora estão ao alcance de qualquer pessoa com um smartphone e um pouco de curiosidade.&lt;/p&gt;&lt;p data-end=&quot;4448&quot; data-start=&quot;4394&quot;&gt;Você não precisa se tornar um escravo do controle. Pelo contrário: ao automatizar o que é chato, repetitivo e sujeito a falhas humanas (como a força de vontade), você libera sua mente e seu tempo para o que realmente importa: viver a vida que o seu dinheiro, bem gerido, pode proporcionar.&lt;/p&gt;&lt;p data-end=&quot;4448&quot; data-start=&quot;4394&quot;&gt;Comece hoje. Escolha um dos 5 erros e implemente a correção. O seu futuro eu agradece.&lt;/p&gt;
&lt;p data-end=&quot;4920&quot; data-start=&quot;4722&quot;&gt;&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://professorcarlos.blogspot.com/feeds/34063633434141020/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/33774862/34063633434141020?isPopup=true' title='0 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/34063633434141020'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/34063633434141020'/><link rel='alternate' type='text/html' href='http://professorcarlos.blogspot.com/2025/11/5-erros-que-sabotam-suas-financas-mesmo-que-voce-ganhe-bem.html' title='5 erros que sabotam suas finanças mesmo que você ganhe bem'/><author><name>Professor Carlos</name><uri>http://www.blogger.com/profile/10347668022241306511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFBZD8558AE67R-CINkytW3INbUINaAhDuZ7GKm_4DSSKeHSpdm3K-TPQrhNPn5B-21wN24ZH8flimqC7ikTzmRrYZBAsyD_JSC8YW8BDJZwmTby9R7LZC9SYkyHkSs7NPVZxXiqEDqjhkRJNxZwqqyZaVgYjISiFpzXsSyGZmL1YFCQ/s220/WhatsApp%20Image%202024-05-17%20at%2017.42.05.jpeg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVtAPu9GCwRV6_euJ8dgurtW9AJs-bbjtplcekZOHUor79y7RVvoYJ59V0lGJWo1eBmo4lJyKHx6XM311AYHprZnvwq5bPdK3QMJRrUMDWeHU6E1kXQHfg7AiQX75TZuN1-LCu9G4vuPMDcTiKcCEYWfICo19wYZa_2F0U5HHRIA0JsIzN1djA/s72-w640-h426-c/beautiful-freelancer-stock-broker-signing-business-deal-her-work-desk-successful-businesswoman-reading-work-contract.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-33774862.post-2706009230487808994</id><published>2025-11-22T08:22:00.006-03:00</published><updated>2025-12-13T18:35:10.929-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="assistente financeiro pessoal"/><category scheme="http://www.blogger.com/atom/ns#" term="automação"/><category scheme="http://www.blogger.com/atom/ns#" term="finanças pessoais"/><category scheme="http://www.blogger.com/atom/ns#" term="IA"/><category scheme="http://www.blogger.com/atom/ns#" term="inteligência artificial"/><title type='text'>Seu Assistente Financeiro Pessoal no Telegram: Integrando IA ao Bot do Google Sheets</title><content type='html'>&lt;div class=&quot;post-header&quot;&gt;
&lt;h1 style=&quot;text-align: left;&quot;&gt;Seu Assistente Financeiro Pessoal no Telegram: Integrando IA ao Bot do Google Sheets&lt;/h1&gt;
  &lt;p class=&quot;post-meta&quot;&gt;
    Tempo de leitura: 10 minutos
  &lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;Criar um assistente financeiro pessoal pode ser um grande desafio. Mas com a combinação de &lt;strong data-end=&quot;463&quot; data-start=&quot;453&quot;&gt;Python&lt;/strong&gt;, &lt;strong data-end=&quot;485&quot; data-start=&quot;465&quot;&gt;Telegram Bot API&lt;/strong&gt;, &lt;strong data-end=&quot;504&quot; data-start=&quot;487&quot;&gt;Google Sheets&lt;/strong&gt; e &lt;strong data-end=&quot;517&quot; data-start=&quot;507&quot;&gt;OpenAI&lt;/strong&gt;, você pode construir uma ferramenta capaz de analisar despesas e receitas, organizar planilhas, gerar diagnósticos financeiros automáticos usando IA e até ajudar outras pessoas a cuidar do dinheiro.&amp;nbsp;&lt;/p&gt;&lt;p&gt;Neste artigo, você vai aprender passo a passo &lt;b&gt;como integrar uma Inteligência Artificial ao&amp;nbsp;&lt;/b&gt;&lt;strong data-end=&quot;857&quot; data-start=&quot;810&quot;&gt;bot de Telegram criado no &lt;a href=&quot;https://professorcarlos.blogspot.com/2025/11/como-criar-um-bot-telegram-para-atualizar-planilha-financeira-Google-Sheets.html&quot; target=&quot;_blank&quot;&gt;post anterior&lt;/a&gt;&lt;/strong&gt;, a qual será capaz de atuar como um verdadeiro assistente financeiro.&amp;nbsp;&lt;/p&gt;&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOhf-gX7qPDI0ZoaJI5Ks7mZHYbhb7yYFKCYH2PVWMmpKY7ngjXtycwe7NjZpgK6uik6iwufUJkMY2U4V5IM4D7kCFwShsooRaF30HQ0Se1KIPasz992isP9nkWe6hNqdcwOo6LrL0zbtghGS45xVlHu2pF4vRBHS2PhJw7rV0Qy0sxJ1wYvQO/s4096/tecnologia-e-holograma-ilustrados.jpg&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;2649&quot; data-original-width=&quot;4096&quot; height=&quot;414&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOhf-gX7qPDI0ZoaJI5Ks7mZHYbhb7yYFKCYH2PVWMmpKY7ngjXtycwe7NjZpgK6uik6iwufUJkMY2U4V5IM4D7kCFwShsooRaF30HQ0Se1KIPasz992isP9nkWe6hNqdcwOo6LrL0zbtghGS45xVlHu2pF4vRBHS2PhJw7rV0Qy0sxJ1wYvQO/w640-h414/tecnologia-e-holograma-ilustrados.jpg&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Fonte:&amp;nbsp;https://br.freepik.com/&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Pré-requisitos&lt;/h2&gt;&lt;div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;Recomendamos que você já tenha criado o Bot Telegram descrito &lt;a href=&quot;https://professorcarlos.blogspot.com/2025/11/como-criar-um-bot-telegram-para-atualizar-planilha-financeira-Google-Sheets.html&quot; target=&quot;_blank&quot;&gt;neste post&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Bibliotecas &lt;i&gt;pandas&lt;/i&gt; e&amp;nbsp;&lt;i&gt;openai&lt;/i&gt;&lt;/li&gt;&lt;li&gt;Conta na OpenAI&lt;/li&gt;&lt;/ul&gt;Em posts anteriores aprendemos como criar um Bot Telegram, que faz registros de transações numa planilha Google Sheets. Vamos utilizar este bot e implementar um &lt;i&gt;upgrade&lt;/i&gt;, que adiciona a funcionalidade da ferramenta fazer um diagnóstico das nossas finanças e oferecer &lt;i&gt;insights&lt;/i&gt; com base nesse diagnóstico. Essa nova funcionalidade do bot será implementada utilizando uma IA, mais especificamente um modelo da OpenAI.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Considerando que vamos partir do código funcionando, criado no artigo anterior, será necessário instalar as bibliotecas:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;pip&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;install&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;openai&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;pandas&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Desde que você já possua uma conta, a fim de obter um token da API OpenAI, precisamos criá-lo no &lt;a href=&quot;https://platform.openai.com/api-keys&quot; target=&quot;_blank&quot;&gt;dashboard da plataforma&lt;/a&gt;.
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;border: 1px solid rgb(0, 0, 0);text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDy3yzkeLsBNQEHKqTxjWhvAsOtSoM20tM59NW60lqDowo2h1OIDJnN4id9GXdqvpjM9H9Qz7d-jxWUO810nYbsQj2LUNEmBuhtNjzkiQ1pzrwrx0m0yhGxR9BjPr6Or2pzYN19rOjWNPRclygpJNvg80XjtNlWQhERqUvmH8XY8573397MApZ/s1109/dashboard.jpg&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;518&quot; data-original-width=&quot;1109&quot; height=&quot;298&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDy3yzkeLsBNQEHKqTxjWhvAsOtSoM20tM59NW60lqDowo2h1OIDJnN4id9GXdqvpjM9H9Qz7d-jxWUO810nYbsQj2LUNEmBuhtNjzkiQ1pzrwrx0m0yhGxR9BjPr6Or2pzYN19rOjWNPRclygpJNvg80XjtNlWQhERqUvmH8XY8573397MApZ/w640-h298/dashboard.jpg&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Dashboard da OpenAI Platform&lt;br /&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;div&gt;Clique no botão &lt;b&gt;Create new secret key&lt;/b&gt; no canto superior direito.&amp;nbsp;Opcionalmente, você pode dar um nome à sua chave para identificação (ex: &quot;Meu Projeto BOT&quot;) e, em seguida, clique em &lt;b&gt;Create key&lt;/b&gt;.&amp;nbsp;O seu token (chave secreta) será exibido em uma janela. Copie-o imediatamente e guarde-o em um local seguro, pois, por motivos de segurança, você não conseguirá vê-lo novamente depois de fechar a janela.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;A utilização da API da OpenAI geralmente envolve custos baseados no uso (pago por token/uso), e pode ser necessário adicionar um método de pagamento à sua conta para utilizar os serviços. Por exemplo, cerca de 1.000 tokens pode ter um custo $ 0.50, dependendo do modelo utilizado. Para saber mais sobre como os tokens são contados e precificados &lt;a href=&quot;https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them&quot; target=&quot;_blank&quot;&gt;leia aqui&lt;/a&gt;.&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Um pouco mais sobre segurança&lt;/h2&gt;&lt;div&gt;Num &lt;a href=&quot;https://professorcarlos.blogspot.com/2025/11/como-enviar-notificacoes-por-e-mail-ou-pelo-telegram.html&quot; target=&quot;_blank&quot;&gt;post anterior&lt;/a&gt; introduzimos a utilização de &lt;i&gt;dotenv&lt;/i&gt; para guardar as chaves secretas. Esse será o mecanismo utilizado também para guardar nossa chave da API da OpenAI. Neste &lt;i&gt;upgrade&lt;/i&gt; do nosso bot vamos passar a guardar as credenciais da conta de serviço do Google Cloud que aprendemos a criar &lt;a href=&quot;https://professorcarlos.blogspot.com/2025/11/como-automatizar-suas-financas-pessoais.html&quot; target=&quot;_blank&quot;&gt;neste artigo&lt;/a&gt;, também no arquivo &lt;i&gt;.env&lt;/i&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;A dica consiste em criar uma chave no &lt;i&gt;.env&lt;/i&gt;, por exemplo:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;CREDENCIAIS_GOOGLE_SHEETS&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;type&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;service_account&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;project_id&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;xxxxxxxxxxxxxxxxxxxxxx&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;private_key_id&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;private_key&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;-----BEGIN PRIVATE KEY-----&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;O nome da chave pode ser qualquer um, e o valor é o conteúdo do arquivo &lt;i&gt;json&lt;/i&gt; que você baixou quando criou a chave. Sendo que o conteúdo deve estar em forma de string &lt;i&gt;json&lt;/i&gt;. Para isso ponha o conteúdo em uma única linha.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Agora precisamos modificar o arquivo &lt;i&gt;utils.py&lt;/i&gt; que foi criado no &lt;a href=&quot;https://professorcarlos.blogspot.com/2025/11/como-criar-um-bot-telegram-para-atualizar-planilha-financeira-Google-Sheets.html&quot; target=&quot;_blank&quot;&gt;post anterior&lt;/a&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;utils.py&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;CREDENCIAIS&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;os&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;getenv(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;CREDENCIAIS_GOOGLE_SHEETS&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;conectar_google_sheets&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;():&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;scopes&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;[&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;https://www.googleapis.com/auth/spreadsheets&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;https://www.googleapis.com/auth/drive&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;

    &lt;span style=&quot;color: #6272a4;&quot;&gt;# creds = Credentials.from_service_account_file(&lt;/span&gt;
    &lt;span style=&quot;color: #6272a4;&quot;&gt;#     &quot;credenciais.json&quot;, scopes=scopes)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;json_credencials&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;json&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;loads(CREDENCIAIS)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;creds&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;Credentials&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;from_service_account_info(&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;json_credencials,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;scopes&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;scopes)&lt;/span&gt;

    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;client&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;gspread&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;authorize(creds)&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;client&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Note que incluimos uma linha que obtém &lt;i&gt;CREDENCIAIS&lt;/i&gt; do arquivo &lt;i&gt;.env&lt;/i&gt; e, na função &lt;i&gt;conectar_google_sheets()&lt;/i&gt;, substituimos a linha que define &lt;i&gt;creds&lt;/i&gt;. Antes ela precisava ler o arquivo &lt;i&gt;credenciais.json&lt;/i&gt; e agora utilizamos &lt;i&gt;json.loads()&lt;/i&gt; para converter a string &lt;i&gt;CREDENCIAIS&lt;/i&gt; em &lt;i&gt;json&lt;/i&gt; e definimos &lt;i&gt;creds&lt;/i&gt; para buscar as informações desse &lt;i&gt;json&lt;/i&gt;.&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;O que vamos criar&lt;/h2&gt;&lt;div&gt;Iremos incluir o comando &lt;i&gt;/diagnostic&lt;/i&gt; ao código do Bot Telegram criado no artigo anterior, o qual será responsável por receber a planilha de transações, gerar um resumo e com base nesse resumo, a IA irá fazer, por meio de um relatório, um diagnóstico e propor algumas recomendações para economizar, reduzir despesas, etc.&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;O resumo financeiro&lt;/h2&gt;&lt;div&gt;O resumo será gerado por meio da função &lt;i&gt;gerar_resumo_financeiro()&lt;/i&gt;, que recebe como parâmetro a planilha de transações convertida em um Dataframe, o que possibilita um tratamento de dados mais facilitado.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;# Outros imports aqui&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; panda &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; pd&lt;/span&gt;

&lt;span style=&quot;color: #6272a4;&quot;&gt;# Algum código aqui&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;gerar_resumo_financeiro&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(df:&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;pd&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;DataFrame)&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;dict&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;&quot;&quot;&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;    Espera colunas: [&#39;Data&#39;,&#39;Descrição&#39;,&#39;Categoria&#39;,&#39;Tipo&#39;,&#39;Valor&#39;, ...]&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;    Tipo é &#39;Receita&#39; ou &#39;Despesa&#39; (case-insensitive)&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;    &quot;&quot;&quot;&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;df&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;df&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;copy()&lt;/span&gt;
    &lt;span style=&quot;color: #6272a4;&quot;&gt;# garantir colunas mínimas&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;required&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Data&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Descrição&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Categoria&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Tipo&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Valor&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;
    &lt;span style=&quot;color: #6272a4;&quot;&gt;# tentar mapear colunas sem acento&lt;/span&gt;
    &lt;span style=&quot;color: #6272a4;&quot;&gt;# assume que o usuário usou cabeçalhos corretos&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;for&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;col&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;required:&lt;/span&gt;
        &lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;col&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;not&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;df&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;columns:&lt;/span&gt;
            &lt;span style=&quot;color: #ff79c6;&quot;&gt;raise&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;ValueError(&lt;/span&gt;
                &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;A coluna obrigatória &#39;{&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;col&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&#39; não foi encontrada na planilha.&quot;&lt;/span&gt;
                &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;

    &lt;span style=&quot;color: #6272a4;&quot;&gt;# converter tipos&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;df[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Valor&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;pd&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;to_numeric(df[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Valor&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;],&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;errors&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;coerce&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;fillna(&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0.0&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;df[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Tipo&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;df[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Tipo&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;astype(&lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;strip()&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;capitalize()&lt;/span&gt;
    &lt;span style=&quot;color: #6272a4;&quot;&gt;# garantir datas&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;try&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;df[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Data&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;pd&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;to_datetime(df[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Data&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;],&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;dayfirst&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=True&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;errors&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;coerce&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;except&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;Exception:&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;df[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Data&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;pd&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;to_datetime(df[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Data&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;],&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;errors&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;coerce&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;

    &lt;span style=&quot;color: #6272a4;&quot;&gt;# calcular métricas básicas&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;receitas&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;df&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;loc[df[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Tipo&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Receita&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Valor&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;sum()&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;despesas&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;df&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;loc[df[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Tipo&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Despesa&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Valor&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;sum()&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;saldo&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;receitas&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;-&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;despesas&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;taxa_poupanca_pct&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;(saldo&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;/&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;receitas&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: #ffb86c;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;receitas&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: #ffb86c;&quot;&gt;0.0&lt;/span&gt;

    &lt;span style=&quot;color: #6272a4;&quot;&gt;# distribuição por categoria (somente despesas)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;despesas_por_cat&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;df&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;loc[df[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Tipo&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Despesa&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;
        &lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;groupby(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Categoria&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Valor&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;
        &lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;sum()&lt;/span&gt;
        &lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;sort_values(ascending&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=False&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;to_dict()&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;

    &lt;span style=&quot;color: #6272a4;&quot;&gt;# dívida detectada: categoria chamada &#39;Dívidas&#39; ou &#39;Dividas&#39;&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;dividas&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ffb86c;&quot;&gt;0.0&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;for&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;key&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Dívidas&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Dividas&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Divida&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Dívida&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]:&lt;/span&gt;
        &lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;key&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;df[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Categoria&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;unique():&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;dividas&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;df&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;loc[&lt;/span&gt;
                &lt;span style=&quot;color: #f8f8f2;&quot;&gt;(df[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Categoria&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;key)&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;(df[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Tipo&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Despesa&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;),&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Valor&#39;&lt;/span&gt;
                &lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;sum()&lt;/span&gt;

    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;resumo&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;receitas&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;round&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(receitas),&lt;/span&gt; &lt;span style=&quot;color: #ffb86c;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;),&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;despesas&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;round&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(despesas),&lt;/span&gt; &lt;span style=&quot;color: #ffb86c;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;),&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;saldo&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;round&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(saldo),&lt;/span&gt; &lt;span style=&quot;color: #ffb86c;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;),&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;taxa_poupanca_pct&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;round&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(taxa_poupanca_pct),&lt;/span&gt; &lt;span style=&quot;color: #ffb86c;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;),&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;despesas_por_categoria&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(k):&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(v)&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;for&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;k,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;v&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;despesas_por_cat&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;items()},&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;dividas&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;round&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(dividas),&lt;/span&gt; &lt;span style=&quot;color: #ffb86c;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;),&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;periodo_inicio&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(df[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Data&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;min())&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt;
            &lt;span style=&quot;color: #ff79c6;&quot;&gt;not&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;df[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Data&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;isnull()&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;all()&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;periodo_fim&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(df[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Data&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;max())&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt;
            &lt;span style=&quot;color: #ff79c6;&quot;&gt;not&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;df[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Data&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;isnull()&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;all()&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;resumo&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Antes de criar o resumo, a função checa se a planilha tem as colunas obrigatórias. Não importa a ordem, se estão em maiúsculas ou não ou se possuem acentuação.&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Em seguida é feita a conversão de tipos, tanto os numéricos quanto as strings, e a formatação adequada das datas. Tudo isso para assegurar a criação do resumo, visto que, por exemplo, será necessário totalizar cada categoria, e isso exige que cada uma delas tenha denominação padronizada na planilha inteira.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Finalmente, a função cria e retorna o resumo na forma de um &lt;i&gt;dict&lt;/i&gt;.&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;O prompt&lt;/h2&gt;&lt;div&gt;O prompt que será enviado ao modelo é montado na função &lt;i&gt;montar_prompt_para_openai()&lt;/i&gt;. A função recebe o resumo criado na função anterior e o envia no prompt. Note que o prompt é bastante específico, detalhando o que se quer da IA. No entanto não é um texto fechado, você sempre pode e deve melhorá-lo.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;montar_prompt_para_openai&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(resumo:&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;dict&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
    &lt;span style=&quot;color: #6272a4;&quot;&gt;# Template em PT-BR para o modelo receber e gerar diagnóstico&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;prompt&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;&quot;&quot;&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;    Você é um especialista em finanças pessoais. Analise o resumo financeiro&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;    abaixo e gere um relatório de diagnóstico completo, claro e motivador.&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;    Divida o relatório em: Visão geral, Principais pontos de atenção,&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;    Oportunidades de economia, Plano de ação (3 a 5 passos) e Recomendação&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;    de produtos/contas para reserva de emergência.&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;    Seja prático e dê números concretos (valores em reais e percentuais).&lt;/span&gt;

&lt;span style=&quot;color: #bd93f9;&quot;&gt;    Resumo financeiro (auto-gerado):&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;    - Período: {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;resumo&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;get(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;periodo_inicio&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;} até {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;resumo&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;get(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;periodo_fim&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;    - Receitas totais: R$ {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;resumo&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;get(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;receitas&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;:.2f}&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;    - Despesas totais: R$ {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;resumo&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;get(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;despesas&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;:.2f}&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;    - Saldo: R$ {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;resumo&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;get(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;saldo&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;:.2f}&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;    - Taxa de poupança (% sobre a receita):&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;        {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;resumo&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;get(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;taxa_poupanca_pct&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;:.2f}%&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;    - Dívidas identificadas (valor): R$ {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;resumo&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;get(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;dividas&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;:.2f}&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;    - Distribuição das maiores categorias de despesa:&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;        {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;resumo&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;get(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;despesas_por_categoria&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&lt;/span&gt;

&lt;span style=&quot;color: #bd93f9;&quot;&gt;    Dê recomendações específicas com valores (ex.: &quot;reduza X na categoria Y,&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;    isso economiza R$ Z por mês&quot;) e proponha metas (ex.: reserva de emergência&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;    equivalente a N meses de despesas).&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;    &quot;&quot;&quot;&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;prompt&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;A função retorna então esse prompt detalhado, o qual contém informações do resumo.&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;O comando &lt;i&gt;/diagnostic&lt;/i&gt;&lt;/h2&gt;&lt;div&gt;O primeiro passo na função &lt;i&gt;diagnostic_command()&lt;/i&gt; é conectar ao Google Sheets e ler a planilha alvo no formato de um &lt;i&gt;list&lt;/i&gt; de &lt;i&gt;dict&lt;/i&gt;. Em seguida, por meio de &lt;i&gt;pd.DataFrame()&lt;/i&gt;, converter esse &lt;i&gt;list&lt;/i&gt; em um DataFrame.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;async&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;diagnostic_command&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;update:&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;Update,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;context:&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;ContextTypes&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;DEFAULT_TYPE)&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;cliente&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;conectar_google_sheets()&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;planilha&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;cliente&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;open(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Minhas Finanças Pessoais&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;sheet&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;planilha&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;worksheet(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Transações&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;registros&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;sheet&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;get_all_records()&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;df&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;pd&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;DataFrame(registros)&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;try&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;resumo&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;gerar_resumo_financeiro(df)&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;except&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;Exception&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;e:&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;logger&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;exception(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Erro no resumo financeiro&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: #ff79c6;&quot;&gt;await&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;update&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;message&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;reply_text(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;Erro ao processar os dados: {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;prompt&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;montar_prompt_para_openai(resumo)&lt;/span&gt;
    &lt;span style=&quot;color: #6272a4;&quot;&gt;# chamada básica para OpenAI (Chat Completions)&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;try&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;response&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;client&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;chat&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;completions&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;create(&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;model&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;gpt-3.5-turbo&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;messages&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;[&lt;/span&gt;
                &lt;span style=&quot;color: #f8f8f2;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;developer&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
                 &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;                     &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;&quot;&quot;Você é um assistente especialista&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;                     em finanças pessoais.&quot;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;},&lt;/span&gt;
                &lt;span style=&quot;color: #f8f8f2;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;role&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;user&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;content&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;prompt}&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;],&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;max_tokens&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;800&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;temperature&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0.3&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;texto_relatorio&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;response&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;choices[&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;message&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;content&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;strip()&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;except&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;Exception&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;e:&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;logger&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;exception(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;Erro chamando OpenAI {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: #ff79c6;&quot;&gt;await&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;update&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;message&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;reply_text(&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;Erro ao gerar o relatório via OpenAI {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;e&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;texto_relatorio&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Relatório automático (fallback):\n\n&quot;&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;Receitas: R$ {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;resumo[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;receitas&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}\n&quot;&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;Despesas: R$ {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;resumo[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;despesas&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}\n&quot;&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;Saldo: R$ {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;resumo[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;saldo&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}\n&quot;&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;Taxa poupança: {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;resumo[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;taxa_poupanca_pct&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}%\n&quot;&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;

    &lt;span style=&quot;color: #6272a4;&quot;&gt;# enviar resultados&lt;/span&gt;
    &lt;span style=&quot;color: #6272a4;&quot;&gt;# primeiro um resumo curto&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;resumo_msg&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;✅ Diagnóstico pronto!\n\n&quot;&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;Receitas: R$ {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;resumo[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;receitas&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;:.2f}\n&quot;&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;Despesas: R$ {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;resumo[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;despesas&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;:.2f}\n&quot;&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;Saldo: R$ {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;resumo[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;saldo&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;:.2f}\n&quot;&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;Taxa de poupança: {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;resumo[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;taxa_poupanca_pct&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;:.2f}%\n\n&quot;&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Relatório completo em anexo (PDF) e abaixo em texto.&quot;&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;await&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;update&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;message&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;reply_text(resumo_msg)&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;await&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;update&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;message&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;reply_text(texto_relatorio)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;A seguir é chamada a função &lt;i&gt;gerar_resumo_financeiro()&lt;/i&gt; passando o DataFrame como argumento. Esta função retorna o resumo, o qual é passado para a função &lt;i&gt;montar_prompt_para_openai()&lt;/i&gt;. Esta função retorna o prompt montado.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Com o prompt disponível, é chamada a função&amp;nbsp;&lt;i&gt;client.chat.completions.create()&lt;/i&gt;. Esta função é uma chamada à API da OpenAI, que pode interagir com vários modelos da família GPT, bastando especificá-lo no parâmetro&lt;i&gt; model&lt;/i&gt;. Além do parâmetro &lt;i&gt;model&lt;/i&gt;, é obrigatório o envio de &lt;i&gt;messages&lt;/i&gt;, que é uma lista das mensagens da conversa. &lt;i&gt;Messages&lt;/i&gt; deverá ter um item de &lt;i&gt;developer&lt;/i&gt; e outro de &lt;i&gt;user&lt;/i&gt;, no qual passamos o prompt que foi montado antes. O parâmetro &lt;i&gt;max_tokens&lt;/i&gt; é usado para controlar os custos das respostas. Por sua vez, &lt;i&gt;temperature&lt;/i&gt; determina a aleatoriedade da resposta. Quanto menor o valor, mais focada é a resposta. Seu valor deve estar em 0 e 2.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Para finalizar, o &lt;i&gt;handler&lt;/i&gt; do comando &lt;i&gt;/diagnostic&lt;/i&gt; é registrado no bot.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Código completo disponível no &lt;a href=&quot;https://github.com/stonefullstm/finance-bot-ai&quot; target=&quot;_blank&quot;&gt;GitHub&lt;/a&gt;.&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Conclusão&lt;/h2&gt;&lt;div&gt;Agora temos um bot pessoal no Telegram, que facilita o trabalho de lançar as transações na planilha e se integra a uma Inteligência Artificial para, com base nessa planilha, fazer uma análise desses dados e fornecer recomendações a fim de aprimorarmos a getão das nossas finanças.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Esse código é apenas um ponto de partida. Uma das melhorias que podemos implementar é pedir à nossa IA fazer o lançamento na planilha a partir de uma mensagem não estruturada. Por exemplo, podemos enviar a mensagem&lt;i&gt;&amp;nbsp;paguei 150,00 num almoço&lt;/i&gt;. E a partir disso modelo infere categorias, descrições, valores, etc.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://professorcarlos.blogspot.com/feeds/2706009230487808994/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/33774862/2706009230487808994?isPopup=true' title='0 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/2706009230487808994'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/2706009230487808994'/><link rel='alternate' type='text/html' href='http://professorcarlos.blogspot.com/2025/11/seu-assistente-financeiro-pessoal-no-telegram-integrando-ia-ao-bot-do-google-sheets.html' title='Seu Assistente Financeiro Pessoal no Telegram: Integrando IA ao Bot do Google Sheets'/><author><name>Professor Carlos</name><uri>http://www.blogger.com/profile/10347668022241306511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFBZD8558AE67R-CINkytW3INbUINaAhDuZ7GKm_4DSSKeHSpdm3K-TPQrhNPn5B-21wN24ZH8flimqC7ikTzmRrYZBAsyD_JSC8YW8BDJZwmTby9R7LZC9SYkyHkSs7NPVZxXiqEDqjhkRJNxZwqqyZaVgYjISiFpzXsSyGZmL1YFCQ/s220/WhatsApp%20Image%202024-05-17%20at%2017.42.05.jpeg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiOhf-gX7qPDI0ZoaJI5Ks7mZHYbhb7yYFKCYH2PVWMmpKY7ngjXtycwe7NjZpgK6uik6iwufUJkMY2U4V5IM4D7kCFwShsooRaF30HQ0Se1KIPasz992isP9nkWe6hNqdcwOo6LrL0zbtghGS45xVlHu2pF4vRBHS2PhJw7rV0Qy0sxJ1wYvQO/s72-w640-h414-c/tecnologia-e-holograma-ilustrados.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-33774862.post-9041671957148270661</id><published>2025-11-17T08:41:00.008-03:00</published><updated>2025-11-27T16:09:47.063-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="automação"/><category scheme="http://www.blogger.com/atom/ns#" term="bot telegram"/><category scheme="http://www.blogger.com/atom/ns#" term="finanças pessoais"/><category scheme="http://www.blogger.com/atom/ns#" term="google sheets"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><title type='text'>Como criar um Bot Telegram para atualizar uma planilha financeira no Google Sheets</title><content type='html'>&lt;div class=&quot;post-header&quot;&gt;
&lt;h1&gt;Como criar um Bot Telegram para atualizar uma planilha financeira no Google Sheets&lt;/h1&gt;
  &lt;p class=&quot;post-meta&quot;&gt;
    Tempo de leitura: 10 minutos
  &lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;&lt;a href=&quot;https://professorcarlos.blogspot.com/2025/11/como-automatizar-suas-financas-pessoais.html&quot; target=&quot;_blank&quot;&gt;Nesse post&lt;/a&gt; nós mostramos como automatizar uma planilha financeira pessoal usando Python. Vimos como atualizar as transações a partir de um arquivo de extrato bancário em &lt;i&gt;csv&lt;/i&gt;. Todavia, muitas transações no nosso dia a dia não são feitas por meio de um banco. Podem ser feitas em dinheiro, ou usando cartão de crédito. E seja numa planilha como foi apresentado, ou utilizando um &lt;i&gt;app&lt;/i&gt; de finanças pessoais, fazer esses lançamentos no momento que ocorrem pode demandar algum tempo, e se deixarmos para fazer posteriormente, isso é passível de esquecimento. E se pudéssemos fazer isso por meio do envio de uma mensagem instantânea? Neste artigo iremos mostrar como criar um bot Telegram usando Python, que recebe essa mensagem e faz o registro da transação na nossa planilha.&lt;/p&gt;&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhhtHtXfekwvdpS-qUYGKJ-JBRxZVM70hstqt0Bx72cpousZrgPrmD3AjEAS5_nirV23ElgdN5hvqVbo5C9mp8vcnvfS3tkBvbqepWSvOX2QHYbu_QuHfkCHWd16L2vbcrjIqgmjU3z9UzUeHf6RjeO6soQLoXV_93FCvHPjXEoYS-YGTwFj5Fa/s7418/18303719.jpg&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;4000&quot; data-original-width=&quot;7418&quot; height=&quot;345&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhhtHtXfekwvdpS-qUYGKJ-JBRxZVM70hstqt0Bx72cpousZrgPrmD3AjEAS5_nirV23ElgdN5hvqVbo5C9mp8vcnvfS3tkBvbqepWSvOX2QHYbu_QuHfkCHWd16L2vbcrjIqgmjU3z9UzUeHf6RjeO6soQLoXV_93FCvHPjXEoYS-YGTwFj5Fa/w640-h345/18303719.jpg&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Fonte:&amp;nbsp;https://br.freepik.com/&lt;br /&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Pré-requisitos&lt;/h2&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;ul&gt;&lt;li&gt;Conta no Google e credenciais no Google Cloud&lt;/li&gt;&lt;li&gt;Python e bibliotecas instaladas&lt;/li&gt;&lt;li&gt;Bot Telegram&lt;/li&gt;&lt;/ul&gt;Já vimos num &lt;a href=&quot;https://professorcarlos.blogspot.com/2025/11/como-automatizar-suas-financas-pessoais.html&quot; target=&quot;_blank&quot;&gt;post anterior&lt;/a&gt; como criar credenciais no Google Cloud e também o código Python que faz essa conexão com nossa planilha. Já, neste &lt;a href=&quot;https://professorcarlos.blogspot.com/2025/11/como-enviar-notificacoes-por-e-mail-ou-pelo-telegram.html&quot; target=&quot;_blank&quot;&gt;outro artigo&lt;/a&gt;, mostramos como criar um Bot Telegram. Aqui, vamos utilizar o mesmo bot.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;Instale as bibliotecas que serão necessárias:&lt;div&gt;&lt;br /&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;pip&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;install&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;python&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;dotenv&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;python&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;telegram&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;bot&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;gspread&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;google&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;auth&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Iniciando a implementação&lt;/h2&gt;&lt;div&gt;Um script para bot Telegram deve ficar executando e a determinado intervalo de tempo, checa se o usuário interagiu enviando algum comando. Se o comando enviado for válido, ou seja, estiver entre os comandos previstos no script, o bot irá responder enviando uma simples mensagem ou executando uma ação mais complexa. Neste script inicial, nosso bot, responde com uma mensagem de boas vindas ao comando &lt;i&gt;/start&lt;/i&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;code-header&quot;&gt;finance_bot.py&lt;/div&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; dotenv &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;load_dotenv&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; os&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; telegram &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;Update&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; telegram.ext &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;ApplicationBuilder,&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;CommandHandler,&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;ContextTypes)&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;load_dotenv()&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;TELEGRAM_TOKEN&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;os&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;getenv(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;TELEGRAM_TOKEN&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;


&lt;span style=&quot;color: #6272a4;&quot;&gt;# Handlers&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;async&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;start&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(update:&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;Update,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;context:&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;ContextTypes&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;DEFAULT_TYPE)&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;await&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;update&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;message&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;reply_text(&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Seja bemvindo ao Finance Bot! &quot;&lt;/span&gt;
         &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Envie /help para ver os comandos disponíveis.&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;

&lt;span style=&quot;color: #6272a4;&quot;&gt;# Os demais handlers serão inseridos aqui&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;main&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;():&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;not&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;TELEGRAM_TOKEN:&lt;/span&gt;
        &lt;span style=&quot;color: #ff79c6;&quot;&gt;raise&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;RuntimeError(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Defina as variáveis de ambiente TELEGRAM_TOKEN.&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;

    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;app&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;ApplicationBuilder()&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;token(TELEGRAM_TOKEN)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;build()&lt;/span&gt;

    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;add_handler(&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;CommandHandler(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;start&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;start))&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;run_polling()&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;__name__&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;main()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;A fim de implementar o script do bot iremos precisar do &lt;i&gt;token&lt;/i&gt; que você anotou quando criou o seu bot seguindo os passos que foram descritos em post anterior. Recomendamos que este &lt;i&gt;token&lt;/i&gt; e outros dados sensíveis sejam mantidos em um arquivo &lt;i&gt;.env&lt;/i&gt; e sejam utilizados em nosso programa por meio da biblioteca &lt;i&gt;dotenv&lt;/i&gt;, assim como explicamos &lt;a href=&quot;https://professorcarlos.blogspot.com/2025/11/como-enviar-notificacoes-por-e-mail-ou-pelo-telegram.html&quot; target=&quot;_blank&quot;&gt;nesse post&lt;/a&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Após essas definições iniciais, tais como os &lt;i&gt;imports e&lt;/i&gt;&amp;nbsp;a inicialização do &lt;i&gt;token&lt;/i&gt;, partimos para a criação dos &lt;i&gt;handlers&lt;/i&gt;, que é como chamamos as funções que devem ser executadas em resposta aos comandos.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;O primeiro &lt;i&gt;handler&lt;/i&gt; que implementamos é o &lt;i&gt;start&lt;/i&gt;, que será executado quando enviarmos o comando &lt;i&gt;/start&lt;/i&gt;. Não é necessário que o nome do &lt;i&gt;handler&lt;/i&gt; seja exatamente o mesmo nome do comando. Mais adiante explicamos o porquê.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Na assinatura da função observe que temos uma palavra-chave &lt;i&gt;async&lt;/i&gt;. Essa palavra-chave define que uma função é assíncrona, ou seja, que executa de modo concorrente, sem bloquear a &lt;i&gt;thread&lt;/i&gt; principal. Geralmente as chamadas à API, acessos a banco de dados, entre outros, precisam desse cuidado, pois são tarefas que demandam tempo para responder.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Ainda no contexto da função &lt;i&gt;start()&lt;/i&gt;, a chamada &lt;i&gt;reply_text()&lt;/i&gt;, que envia uma mensagem ao usuário, é precedida de um &lt;i&gt;await&lt;/i&gt;, que serve para pausar a execução da função até que esta chamada seja resolvida. Um &lt;i&gt;await&lt;/i&gt; só pode ser utilizado no contexto de uma função &lt;i&gt;async&lt;/i&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Depois dos &lt;i&gt;handlers&lt;/i&gt;, temos a função &lt;i&gt;main()&lt;/i&gt;, que efetivamente irá executar o bot. Ela que cria uma instância do bot, adiciona os &lt;i&gt;handlers&lt;/i&gt; e executa o script.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Antes de qualquer coisa nós verificamos se há um &lt;i&gt;token&lt;/i&gt; definido, depois criamos a instância do bot. Na linha seguinte adicionamos o &lt;i&gt;handler&lt;/i&gt; que foi definido. Note que a classe &lt;i&gt;CommandHandler&lt;/i&gt; tem dois parâmetros: uma string, que será o comando ao qual o bot irá responder, e um segundo parâmetro que é o nome da função &lt;i&gt;handler&lt;/i&gt;. Daí o porquê de comando e função não precisarem ter o mesmo nome.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Finalmente, chamamos o método &lt;i&gt;run_polling()&lt;/i&gt;, que inicia o bot e o mantém executando continuamente buscando atualizações do servidor Telegram. Esse método bloqueia a &lt;i&gt;thread&lt;/i&gt; corrente até que o processo seja interrompido, teclando CTRL + C.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Execute o script digitando no terminal &lt;i&gt;python finance_bot.py&lt;/i&gt;. Se tudo estiver certo procure o bot pelo nome no Telegram e envie &lt;i&gt;/start&lt;/i&gt; e você deve receber a mensagem de boas vindas.&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Maneiras de executar o Bot Telegram&lt;/h2&gt;&lt;div&gt;Antes de prosseguirmos com o nosso código, precisamos falar das maneiras de manter um bot Telegram executando.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Chamar &lt;i&gt;run_polling() &lt;/i&gt;no terminal é uma maneira de testar nosso código. Assim, é possível checar se o bot está ouvindo as mensagens e respondendo. O uso de &lt;i&gt;polling&lt;/i&gt; é possível fazer em servidores como &lt;a href=&quot;https://render.com/&quot; target=&quot;_blank&quot;&gt;Render&lt;/a&gt;, &lt;a href=&quot;https://railway.com/&quot; target=&quot;_blank&quot;&gt;Railway&lt;/a&gt;, entre outros, de uma maneira bem simples, mas tem o inconveniente de o bot sair do ar se o processo parar ou precisar reiniciar.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Uma maneira mais profissional consiste em utilizar um&lt;i&gt; webhook&lt;/i&gt;. Esse sistema é utilizado para que dois sistemas se comuniquem, e um deles é comunicado quando um evento acontece, em vez de precisar ficar verificando periodicamente se esse evento aconteceu, tal como no &lt;i&gt;polling&lt;/i&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Tem a vantagem de estar sempre disponível, mas precisa de um&amp;nbsp;&lt;i&gt;endpoint&amp;nbsp;&lt;/i&gt;(criado com Flask ou FastAPI, por exemplo) e de um domínio HTTPS e certificado SSL. No próprio Render é possível hospedar um &lt;i&gt;webhook&lt;/i&gt;. Já o&amp;nbsp;&lt;a href=&quot;https://ngrok.com/&quot; target=&quot;_blank&quot;&gt;Ngrok&lt;/a&gt; é um serviço que possibilita que o &lt;i&gt;webhook&lt;/i&gt; fique rodando em nosso computador. De todo modo, assinar esses serviços no plano gratuito tem o inconveniente do serviço &quot;dormir&quot; devido a algum tempo de inatividade e o Telegram pode não esperar (devido o &lt;i&gt;timeout&lt;/i&gt;) e nossa mensagem ao bot vai ficar sem resposta.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Este tema é um pouco avançado e não cabe no escopo deste artigo. Mas se houver interesse podemos apresentá-lo em um post no futuro.&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Escrevendo os demais &lt;i&gt;handlers&lt;/i&gt;&lt;/h2&gt;&lt;div&gt;Os outros handlers do nosso bot deverão responder aos comandos &lt;i&gt;/help&lt;/i&gt;, que exibe uma ajuda sobre os comandos disponíveis, &lt;i&gt;/save&lt;/i&gt;, responsável por registrar um lançamento na planilha, e &lt;i&gt;/last&lt;/i&gt; que vai exibir os últimos 5 lançamentos que foram feitos.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;O código de tais &lt;i&gt;handlers&lt;/i&gt; serão incluídos logo após o &lt;i&gt;start()&lt;/i&gt;. Iniciamos mostrando o &lt;i&gt;help_command()&lt;/i&gt;, o qual é semelhante ao &lt;i&gt;start()&lt;/i&gt;, e não necessita maiores comentários.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;async&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;help_command&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;update:&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;Update,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;context:&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;ContextTypes&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;DEFAULT_TYPE)&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;help_text&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Comandos disponíveis:\n&quot;&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;/start - Iniciar o bot\n&quot;&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;/help - Mostrar esta mensagem de ajuda\n&quot;&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;/save - Salvar um lançamento na planilha\n&quot;&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;   /save valor/categoria/tipo/descrição (descrição é opcional)\n&quot;&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;   Tipo deve ser &#39;Despesa&#39; ou &#39;Receita&#39;\n&quot;&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;   Exemplo: /save 50,00/Alimentação/Despesa/Jantar com amigos\n&quot;&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;/last - Mostrar as últimas 5 transações\n&quot;&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;await&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;update&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;message&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;reply_text(help_text)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Antes de comentarmos o próximo &lt;i&gt;handler&lt;/i&gt;, &lt;i&gt;save_command()&lt;/i&gt;, precisamos falar do módulo &lt;i&gt;utils.py&lt;/i&gt;, que tem duas funções auxiliares. Primeiro,&amp;nbsp;&lt;i&gt;conectar_google_sheets()&lt;/i&gt;, responsável por fazer a conexão com a API do Google Sheets, a qual já utilizamos nos posts anteriores, e &lt;i&gt;normalizar_string()&lt;/i&gt;, que converte strings para um formato específico, o qual possibilita totalizações e comparações na planilha.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;code-header&quot;&gt;utils.py&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; gspread&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; unicodedata&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; os&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; google.oauth2.service_account &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;Credentials&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;conectar_google_sheets&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;():&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;scopes&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;[&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;https://www.googleapis.com/auth/spreadsheets&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;https://www.googleapis.com/auth/drive&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;

    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;creds&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;Credentials&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;from_service_account_file(&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;credenciais.json&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;scopes&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;scopes)&lt;/span&gt;

    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;client&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;gspread&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;authorize(creds)&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;client&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;normalizar_string&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(s:&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;normalized&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;unicodedata&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;normalize(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;NFKD&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;s)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;folded&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;join([c&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;for&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;c&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;normalized&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;not&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;unicodedata&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;combining(c)])&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;folded&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;casefold()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Este módulo necessita que &lt;i&gt;gspread&lt;/i&gt; seja instalado e importado para lidar com planilhas Google. Veja &lt;a href=&quot;https://professorcarlos.blogspot.com/2025/11/como-automatizar-suas-financas-pessoais.html&quot; target=&quot;_blank&quot;&gt;este artigo&lt;/a&gt; para saber mais sobre se conectar à API Google Sheets.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;A função &lt;i&gt;normalizar_string()&lt;/i&gt; precisa do pacote &lt;i&gt;unicodedata&lt;/i&gt;, nativo do Python. Ela é reponsável por substituir caracteres acentuados e cedilha, e converter uma string em caixa baixa.&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;# Acrescente estes imports&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; datetime &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;date&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; utils &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;conectar_google_sheets,&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;normalizar_string)&lt;/span&gt;

&lt;span style=&quot;color: #6272a4;&quot;&gt;# Handler save&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;async&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;save_command&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;update:&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;Update,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;context:&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;ContextTypes&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;DEFAULT_TYPE)&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
    &lt;span style=&quot;color: #6272a4;&quot;&gt;# Verifica se o usuário forneceu argumentos&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;not&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;context&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;args:&lt;/span&gt;
        &lt;span style=&quot;color: #ff79c6;&quot;&gt;await&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;update&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;message&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;reply_text(&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Por favor, forneça informações a registrar separadas por /.&quot;&lt;/span&gt;
             &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Digite no formato: valor/categoria/tipo/descrição.&quot;&lt;/span&gt;
             &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Descrição é opcional.&quot;&lt;/span&gt;
             &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;
    &lt;span style=&quot;color: #6272a4;&quot;&gt;# Obtém os dados fornecidos pelo usuário e separa-os&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;dados&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;context&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;args[&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;split(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;/&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #6272a4;&quot;&gt;# Verifica se há pelo menos 3 partes (valor, categoria, tipo)&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(dados)&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span style=&quot;color: #ffb86c;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
        &lt;span style=&quot;color: #ff79c6;&quot;&gt;await&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;update&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;message&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;reply_text(&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Formato inválido. Use: valor/categoria/tipo/descrição&quot;&lt;/span&gt;
             &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;
    &lt;span style=&quot;color: #6272a4;&quot;&gt;# Valida o valor fornecido&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;valor&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;dados[&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;replace(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;,&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;.&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;try&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
        &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(valor)&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;except&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;ValueError:&lt;/span&gt;
        &lt;span style=&quot;color: #ff79c6;&quot;&gt;await&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;update&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;message&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;reply_text(&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;O valor deve ser um número. Por favor, tente novamente.&quot;&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;cliente&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;conectar_google_sheets()&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;planilha&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;cliente&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;open(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Minhas Finanças Pessoais&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;sheet&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;planilha&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;worksheet(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Transações&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #6272a4;&quot;&gt;# Adiciona uma nova linha na planilha com os dados fornecidos&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;sheet&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;append_row(&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;[&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;today()&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;strftime(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;%d/%m/%Y&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;),&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;dados[&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(dados)&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #ffb86c;&quot;&gt;3&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;normalizar_string(dados[&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;])&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;capitalize(),&lt;/span&gt;
            &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(valor),&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;normalizar_string(dados[&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;])&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;capitalize(),&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #6272a4;&quot;&gt;# Dá feedback ao usuário&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;await&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;update&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;message&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;reply_text(&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;&quot;&quot;&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;        Você registrou: {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;dados[&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;} para {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;dados[&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;        no dia {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;today()&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;strftime(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;%d/%m/%Y&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}.&lt;/span&gt;
&lt;span style=&quot;color: #bd93f9;&quot;&gt;        &quot;&quot;&quot;&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;O comando &lt;i&gt;/save&lt;/i&gt; é o mais complexo do nosso bot, até o momento. O comando &lt;i&gt;/help&lt;/i&gt; diz como deve ser o formato de &lt;i&gt;/save&lt;/i&gt; e dá um exemplo: &lt;i&gt;/save 50,00/Alimentação/Despesa/Jantar com amigos&lt;/i&gt;. Ou seja, &lt;i&gt;/save&lt;/i&gt; tem argumentos, que são os dados a serem registrados na planilha. A fim de assegurar um mínimo de estrutura nos dados, são necessárias algumas validações, as quais são tratadas pelo &lt;i&gt;handler&lt;/i&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Assim, o primeiro passo é verificar se foram digitados os arguntos após o &lt;i&gt;/save&lt;/i&gt;. Depois é verificado se existem pelo menos três informações nos argumentos, visto que &lt;i&gt;Descrição&lt;/i&gt; é opcional. Em seguida tratamos o valor numérico da transação, que deve ser um float.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Após tudo validado, a função faz a conexão com a API, e faz um &lt;i&gt;apend_row()&lt;/i&gt; desses dados na planilha. Feito isso, o bot nos dá um feedback, avisando que o registro foi feito.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Finalmente incluimos o &lt;i&gt;handler print_last_transactions()&lt;/i&gt;, o qual irá retornar os cinco últimos registros feitos na planilha.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;async&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;print_last_transactions&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;update:&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;Update,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;context:&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;ContextTypes&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;DEFAULT_TYPE)&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;None&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;cliente&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;conectar_google_sheets()&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;planilha&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;cliente&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;open(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Minhas Finanças Pessoais&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;sheet&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;planilha&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;worksheet(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Transações&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;registros&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;sheet&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;get_all_records()&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;ultimos&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;registros[&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:]&lt;/span&gt;  &lt;span style=&quot;color: #6272a4;&quot;&gt;# Últimas 5 transações&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;mensagem&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Últimas 5 transações:\n&quot;&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;for&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;registro&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;ultimos:&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;mensagem&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;+=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;{&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;registro[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Data&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}: {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;registro[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Tipo&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;} de &quot;&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;{&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;registro[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Valor&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;} em {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;registro[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Categoria&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;} - &quot;&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;{&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;registro[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Descrição&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}\n&quot;&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;await&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;update&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;message&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;reply_text(mensagem)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Esta função se conecta à API, lê todos os registros da planilha em uma lista denominada &lt;i&gt;registros&lt;/i&gt;, e pega os últimos 5. Em seguida, é montada uma string com os dados desses últimos registros e então essa string é exibida como retorno de &lt;i&gt;/last&lt;/i&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;E agora, precisamos adicionar esses &lt;i&gt;handlers&lt;/i&gt; ao bot. Isso é feito na função &lt;i&gt;main()&lt;/i&gt;, incluindo estas linhas logo abaixo de &lt;i&gt;app.add_handler()&lt;/i&gt;:&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;add_handler(&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;CommandHandler(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;help&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;help_command))&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;add_handler(&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;CommandHandler(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;save&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;save_command))&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;app&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;add_handler(&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;CommandHandler(&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;last&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;print_last_transactions))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;O código completo do bot pode ser encontrado no &lt;a href=&quot;https://github.com/stonefullstm/finance-bot&quot; target=&quot;_blank&quot;&gt;GitHub&lt;/a&gt;.&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Conclusão&lt;/h2&gt;&lt;div&gt;Este bot foi projetado com o propósito de facilitar o registro de transações no cotidiano. Alguns cuidados, no entanto, precisamos ter. O primeiro deles é não expor dados sensíveis como senhas, tokens, entre outros. Procuramos orientar o uso de &lt;i&gt;dotenv&lt;/i&gt; para manter esses dados em variáveis de ambiente. Nossas credenciais da API Google Sheets ainda estão em um arquivo, mas em breve mostraremos como passar a tratar esses dados também no &lt;i&gt;dotenv&lt;/i&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Outra preocupação é que, ao você executar o código, seja pelo método &lt;i&gt;polling&lt;/i&gt; ou &lt;i&gt;webhook&lt;/i&gt;, seja no seu próprio computador ou em um serviço de nuvem, qualquer usuário do Telegram pode acessá-lo. Existem maneiras de evitar isso e, mesmo não tendo nos referido a isso no artigo, o código está presente na versão que compartilhamos no GitHub.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Por fim, vamos continuar trabalhando nesse projeto. E a próxima funcionalidade que será implementada consiste de automação com uma IA de modo que ela faça uma análise dos dados na planilha e produza um diagnóstico financeiro.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://professorcarlos.blogspot.com/feeds/9041671957148270661/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/33774862/9041671957148270661?isPopup=true' title='0 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/9041671957148270661'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/9041671957148270661'/><link rel='alternate' type='text/html' href='http://professorcarlos.blogspot.com/2025/11/como-criar-um-bot-telegram-para-atualizar-planilha-financeira-Google-Sheets.html' title='Como criar um Bot Telegram para atualizar uma planilha financeira no Google Sheets'/><author><name>Professor Carlos</name><uri>http://www.blogger.com/profile/10347668022241306511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFBZD8558AE67R-CINkytW3INbUINaAhDuZ7GKm_4DSSKeHSpdm3K-TPQrhNPn5B-21wN24ZH8flimqC7ikTzmRrYZBAsyD_JSC8YW8BDJZwmTby9R7LZC9SYkyHkSs7NPVZxXiqEDqjhkRJNxZwqqyZaVgYjISiFpzXsSyGZmL1YFCQ/s220/WhatsApp%20Image%202024-05-17%20at%2017.42.05.jpeg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhhtHtXfekwvdpS-qUYGKJ-JBRxZVM70hstqt0Bx72cpousZrgPrmD3AjEAS5_nirV23ElgdN5hvqVbo5C9mp8vcnvfS3tkBvbqepWSvOX2QHYbu_QuHfkCHWd16L2vbcrjIqgmjU3z9UzUeHf6RjeO6soQLoXV_93FCvHPjXEoYS-YGTwFj5Fa/s72-w640-h345-c/18303719.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-33774862.post-5052234909306396989</id><published>2025-11-10T17:29:00.011-03:00</published><updated>2025-12-13T18:37:18.916-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="automação"/><category scheme="http://www.blogger.com/atom/ns#" term="finanças pessoais"/><category scheme="http://www.blogger.com/atom/ns#" term="notificações"/><title type='text'>Como enviar notificações por e-mail ou pelo Telegram</title><content type='html'>&lt;div class=&quot;post-header&quot;&gt;
&lt;h1 style=&quot;text-align: left;&quot;&gt;Como enviar notificações por e-mail ou pelo Telegram&lt;/h1&gt;
  &lt;p class=&quot;post-meta&quot;&gt;
    Tempo de leitura: 7 minutos
  &lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;Nos dois últimos posts, mostramos algumas tarefas relacionadas a finanças pessoais, que podem ser agendadas para executar periodicamente e/ou que podem ajudar mais ainda se puderem enviar notificações. Por esse motivo iremos apresentar scripts para automatizar a notificação ao usuário tanto por e-mail (Gmail, no nosso exemplo) quanto por mensagem instantânea (Telegram, principalmente pelo fato do recurso que precisamos ser gratuito).&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjXVVzgWET0q3t-LtOwrLVs0v5yM0cmqhDLMIQIR7vxkYGP9PzwDvwEHuK1rZuqjvf1BsLOmH9CntLnVwJWxaF_bIV13bEhPnwJ_FtZTrGqymvmKi1ZcE_Wo5ia-iFt1nBMTQM33VCPRiylKV47PNvEfNUxg5XbgbYWFCeVs7_NHQJZvZNqXeq/s5000/rede-de-mensagens-de-conexao-de-comunicacao.jpg&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;3146&quot; data-original-width=&quot;5000&quot; height=&quot;402&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjXVVzgWET0q3t-LtOwrLVs0v5yM0cmqhDLMIQIR7vxkYGP9PzwDvwEHuK1rZuqjvf1BsLOmH9CntLnVwJWxaF_bIV13bEhPnwJ_FtZTrGqymvmKi1ZcE_Wo5ia-iFt1nBMTQM33VCPRiylKV47PNvEfNUxg5XbgbYWFCeVs7_NHQJZvZNqXeq/w640-h402/rede-de-mensagens-de-conexao-de-comunicacao.jpg&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Fonte:&amp;nbsp;https://br.freepik.com/&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Mantendo suas chaves em segurança&lt;/h2&gt;&lt;div&gt;Antes de iniciar o assunto objeto deste artigo, é importante falar como cuidar da segurança de chaves, tokens e senhas nas nossas aplicações. Introduzimos este assunto aqui, porque iremos lidar com dados que precisam ser mantidos em sigilo.&lt;/div&gt;&lt;div&gt;O primeiro passo é instalar o pacote &lt;i&gt;dotenv&lt;/i&gt;, uma biblioteca que permite carregar variáveis de ambiente a partir de um arquivo &lt;i&gt;.env&lt;/i&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;pip&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;install&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;python&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;dotenv&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
Em seguida vamos criar um arquivo denominado .env na raiz do projeto. Esse arquivo irá conter variáveis de ambiente no formato NOME_DA_VARIAVEL=VALOR. Por exemplo:&lt;div&gt;&amp;nbsp;&amp;nbsp;&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;EMAIL_USUARIO&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;usuario123456@gmail&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;com&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;EMAIL_SENHA&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;abcdefghijkl&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;TOKEN&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;abcdefghijklmnopqrstuvwxyz&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;CHAT_ID&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;1001943736541&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
Agora criaremos nossos scripts de notificação, quando voltaremos ao &lt;i&gt;dotenv&lt;/i&gt;.
&lt;h2&gt;Script para enviar noticação utilizando o Gmail&lt;/h2&gt;&lt;div&gt;A fim de fazer essa implementação, siga os seguintes passos:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;1) Em primeiro lugar, obtenha uma senha de App seguindo as instruções descritas neste &lt;a href=&quot;https://support.google.com/mail/answer/185833?hl=pt-BR&quot; target=&quot;_blank&quot;&gt;link&lt;/a&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Um requisito para criar uma senha de App é que a verificação em duas etapas esteja ativada na sua conta Google.&lt;/div&gt;&lt;div&gt;Cumprida essa exigência, &lt;a href=&quot;https://myaccount.google.com/apppasswords&quot; target=&quot;_blank&quot;&gt;crie uma senha de App&lt;/a&gt;. Talvez seja pedido para fazer login na sua conta Google. Após isso, irá aparecer essa tela:&lt;/div&gt;&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;border: 1px solid rgb(0, 0, 0);text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjegun2q5KsMVGYBZJ3BcyPaw_i5TmnJYjqIFrfkFAgVHP-5EBE6DE9uWO0fzsjP6qAO-2-n8aB0c6C27zDuiyOUjV5QU-Zz3nLX4EKKO7jbWhxZWQ2tTVz5OElccGbmMeUonBYnR3DBWchJc1yY6ANv5mE11op76Q5bFnW20ESs9APrxnw_QPb/s845/Captura%20de%20tela%20de%202025-11-10%2009-02-55.png&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;628&quot; data-original-width=&quot;845&quot; height=&quot;476&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjegun2q5KsMVGYBZJ3BcyPaw_i5TmnJYjqIFrfkFAgVHP-5EBE6DE9uWO0fzsjP6qAO-2-n8aB0c6C27zDuiyOUjV5QU-Zz3nLX4EKKO7jbWhxZWQ2tTVz5OElccGbmMeUonBYnR3DBWchJc1yY6ANv5mE11op76Q5bFnW20ESs9APrxnw_QPb/w640-h476/Captura%20de%20tela%20de%202025-11-10%2009-02-55.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Tela de criação da senha de App&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;div&gt;Digite o nome do App e clique em &lt;b&gt;Criar&lt;/b&gt;. Será mostrada a senha de 16 dígitos. Copie esse senha agora, ignorando os espaços. Após fechat a tela, você não terá como visualizar ou editá-la, apenas poderá excluí-la.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;2) Em seguida, escreva o script de acordo com nossas instruções.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Crie um arquivo &lt;i&gt;.env&lt;/i&gt; com este conteúdo, na mesma pasta onde se encontra o programa que enviará o e-mail:&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;EMAIL_USUARIO&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;seu&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;email&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;de&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;usuário&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;do&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;Gmail&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;EMAIL_SENHA&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;a&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;senha&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;de&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;App&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;criada,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;sem&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;os&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;espaços&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Nosso script inicia com as seguintes importações e inicializações, considerando que já instalou &lt;i&gt;python-dotenv&lt;/i&gt;:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;# Pacote que permite interagir com SMTP&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; smtplib&lt;/span&gt;
&lt;span style=&quot;color: #6272a4;&quot;&gt;# Pacote que permite lidar com variáveis de ambiente&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; dotenv &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;load_dotenv&lt;/span&gt;
&lt;span style=&quot;color: #6272a4;&quot;&gt;# Pacote para lidar com funções do Sistema Operacional&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; os&lt;/span&gt;

&lt;span style=&quot;color: #6272a4;&quot;&gt;# Lê variáveis do arquivo .env e as define em os.environ&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;load_dotenv()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
E a função completa, que inicializa um servidor SMTP, informa que queremos enviar e-mail de forma segura usando o protocolo TLS, faz login utilizando o e-mail de usuário e a senha de App e finalmente envia mensagem e encerra a instância de SMTP:

&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;code-header&quot;&gt;send_email.py&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; smtplib&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;de&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;ambiente&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; dotenv &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;load_dotenv&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; os&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;load_dotenv()&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;enviar_email&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(assunto,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;mensagem):&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;servidor_smtp&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;smtplib&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;SMTP(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;smtp.gmail.com&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #ffb86c;&quot;&gt;587&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;servidor_smtp&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;starttls()&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;email&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;os&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;getenv(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;EMAIL_USUARIO&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;servidor_smtp&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;login(email,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;os&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;getenv(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;EMAIL_SENHA&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;))&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;servidor_smtp&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;sendmail(&lt;/span&gt;
      &lt;span style=&quot;color: #f8f8f2;&quot;&gt;email,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;email,&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&#39;Subject: {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;assunto&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}\n\n{&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;mensagem&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;servidor_smtp&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;quit()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Neste exemplo, estamos enviando o e-mail para nós mesmos, por isso os dois primeiros parâmetros de &lt;i&gt;sendmail() &lt;/i&gt;são iguais: o remetente e o destinatário são o mesmo usuário.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Agora podemos juntar o script para monitorar cotação de moeda do &lt;a href=&quot;https://professorcarlos.blogspot.com/2025/11/como-automatizar-suas-financas-pessoais.html&quot; target=&quot;_blank&quot;&gt;post anterior&lt;/a&gt; com este para enviar um e-mail de alerta:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; requests&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; send_email &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;enviar_email&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;LIMITE_SUPERIOR&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ffb86c;&quot;&gt;5.3&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;LIMITE_INFERIOR&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ffb86c;&quot;&gt;5.0&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;monitorar_cotacao&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;():&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;API_URL&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;https://economia.awesomeapi.com.br/last/USD-BRL&quot;&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;resp&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;requests&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;get(API_URL)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;data&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;resp&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;json()&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;cotacao_dolar&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(data[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;USDBRL&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;][&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;bid&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;])&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;cotacao_dolar&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;__name__&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;cotacao_atual&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;monitorar_cotacao()&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;cotacao_atual&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;LIMITE_SUPERIOR:&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;enviar_email(&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Alerta de Cotacao&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;A cotacao do dolar subiu para {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;cotacao_atual&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;:.2f} BRL.&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;elif&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;cotacao_atual&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;LIMITE_INFERIOR:&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;enviar_email(&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Alerta de Cotacao&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;A cotacao do dolar caiu para {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;cotacao_atual&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;:.2f} BRL.&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;Script para enviar mensagem pelo Telegram&lt;/h2&gt;&lt;div&gt;Para este script, precisamos seguir estes passos:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;1) Criar um bot no BotFather e obter o token de API&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Inicialmente abra o Telegram e procure o usuário @BotFather. Este usuário ajuda a criar e gerenciar bots Telegram. Se você enviar a mensagem &lt;i&gt;/start&lt;/i&gt;, ele mostrará todos os comandos que ele pode responder. Assim, para criar um novo bot envie a mensagem &lt;i&gt;/newbot&lt;/i&gt;, e siga as instruções. Logo que concluir a criação ele lhe mostrará o token de acesso. Copie-o e o mantenha seguro, pois vamos precisar dele no nosso script.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Em seguida é necessário obter o seu CHAT_ID. Existem algumas alternativas. Uma delas é localizar o usuário @userinfobot e enviar a mensagem &lt;i&gt;/start&lt;/i&gt;. Ele irá responder algumas informações, dentre elas &lt;i&gt;Your ID&lt;/i&gt;. Copie esse número. É o seu CHAT_ID.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;2) Se já tiver instalado &lt;i&gt;python-dotenv&lt;/i&gt;, escreva o scrpit conforme as instruções a seguir:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Crie um arquivo &lt;i&gt;.env&lt;/i&gt; ou, se existir um na mesma pasta onde estará este novo script, abra-o e inclua as seguintes linhas:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;TELEGRAM_TOKEN&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;seu&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;token&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;de&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;API&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;CHAT_ID&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;seu&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;chat_id&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
Instale a biblioteca:&lt;div&gt;&amp;nbsp;&amp;nbsp;&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;pip&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;install&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;python&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;telegram&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;bot&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
E escreva o script:&lt;div&gt;&amp;nbsp;&amp;nbsp;
&lt;div class=&quot;code-header&quot;&gt;send_msg_telegram.py&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;# asyncio é uma biblioteca padrão do Python para programação assíncrona.&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; asyncio&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; telegram&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; dotenv &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;load_dotenv&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; os&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;load_dotenv()&lt;/span&gt;

&lt;span style=&quot;color: #6272a4;&quot;&gt;# Configurações do bot do Telegram&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;TOKEN&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;os&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;getenv(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;TELEGRAM_TOKEN&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;CHAT_ID&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;os&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;getenv(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;CHAT_ID&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;async&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;enviar_mensagem&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(texto):&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;bot&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;telegram&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Bot(token&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;TOKEN)&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;await&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;bot&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;send_message(chat_id&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;CHAT_ID,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;texto)&lt;/span&gt;


&lt;span style=&quot;color: #6272a4;&quot;&gt;# Exemplo de uso&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;mensagem&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Olá! Esta é uma mensagem enviada do Python.&quot;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;asyncio&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;run(enviar_mensagem(mensagem))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
Ao executar esse script você deve receber uma mensagem do bot com nome de usuário que você criou.
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
Agora podemos incrementar a automação que monitora concursos do &lt;a href=&quot;https://professorcarlos.blogspot.com/2025/11/como-automatizar-suas-financas-pessoais.html&quot; target=&quot;_blank&quot;&gt;post anterior&lt;/a&gt;, usando este script para enviar uma notificação pelo Telegram:
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;

&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; requests&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; bs4 &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;BeautifulSoup&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; send_msg_telegram &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;enviar_mensagem&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; asyncio&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;verificar_concursos&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(palavras_chave&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;[]):&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;url&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;https://www.pciconcursos.com.br/&quot;&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;headers&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;User-Agent&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3&quot;&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;response&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;requests&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;get(url,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;headers&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;headers)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;page_contents&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;BeautifulSoup(response&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;text,&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;html.parser&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;

    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;noticias&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;page_contents&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;find(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;div&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;noticias_capa&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #6272a4;&quot;&gt;# Procure por palavras-chaves na página&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;exist&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;any&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(elemento&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;noticias&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;get_text()&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;lower()&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;for&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;elemento&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;palavras_chave)&lt;/span&gt;

    &lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;exist&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;__name__&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;verificar_concursos(&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;[&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;desenvolvedor&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;programador&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;engenheiro de software&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;tecnologia da informação&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;):&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;asyncio&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;run(&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;enviar_mensagem(&lt;/span&gt;
                &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Novo concurso encontrado! Verifique em https://www.pciconcursos.com.br/&quot;&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;h2&gt;Script bônus&lt;/h2&gt;&lt;div&gt;Vamos agendar esses dois scripts utilizando a biblioteca schedule.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;1) Primeiro, instale a biblioteca:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;pip&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;install&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;schedule&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
E assim fica:&lt;div&gt;&amp;nbsp;&amp;nbsp;&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; schedule&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; asyncio&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; time&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; monitorar_concurso &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;verificar_concursos&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; cotacao_moeda &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;monitorar_cotacao&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; send_msg_telegram &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;enviar_mensagem&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;existe_concurso&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;():&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;verificar_concursos(&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;[&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;desenvolvedor&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;programador&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;engenheiro de software&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;tecnologia da informação&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;):&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;asyncio&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;run(&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;enviar_mensagem(&lt;/span&gt;
                &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Novo concurso encontrado! Verifique em https://www.pciconcursos.com.br/&quot;&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;cotacao_dolar&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;():&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;cotacao_atual&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;monitorar_cotacao()&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;cotacao_atual&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #ffb86c;&quot;&gt;5.3&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;asyncio&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;run(&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;enviar_mensagem(&lt;/span&gt;
                &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;A cotacao do dolar subiu para {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;cotacao_atual&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;:.2f} BRL.&quot;&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;elif&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;cotacao_atual&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #ffb86c;&quot;&gt;5.0&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;asyncio&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;run(&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;enviar_mensagem(&lt;/span&gt;
                &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;A cotacao do dolar caiu para {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;cotacao_atual&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;:.2f} BRL.&quot;&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;


&lt;span style=&quot;color: #6272a4;&quot;&gt;# Agendar execuções&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;schedule&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;every()&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;day&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;at(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;17:00&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;do(existe_concurso)&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;schedule&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;every(&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;hours&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;do(cotacao_dolar)&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;while&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;schedule&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;run_pending()&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;time&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;sleep(&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
Agendamos o monitor de concursos para rodar todos os dias às 17:00 e o monitor de cotação para executar de hora em hora.
&lt;h2&gt;Conclusão&lt;/h2&gt;
Certamente que podemos melhorar muito essas automações, mas tome isso como um ponto de partida que você vai incrementando. Por exemplo: as palavras-chaves do monitor de concursos e os limites da cotação de moeda podem ser modificados periodicamente utilizando um arquivo de configuração ou mesmo uma interface de usuário. Usuários avançados podem criar uma API utilizando um framework Python tais como Flask ou FastAPI e assim suas automações ficam rodando sem a necessidade de seu próprio computador estar ligado.</content><link rel='replies' type='application/atom+xml' href='http://professorcarlos.blogspot.com/feeds/5052234909306396989/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/33774862/5052234909306396989?isPopup=true' title='1 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/5052234909306396989'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/5052234909306396989'/><link rel='alternate' type='text/html' href='http://professorcarlos.blogspot.com/2025/11/como-enviar-notificacoes-por-e-mail-ou-pelo-telegram.html' title='Como enviar notificações por e-mail ou pelo Telegram'/><author><name>Professor Carlos</name><uri>http://www.blogger.com/profile/10347668022241306511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFBZD8558AE67R-CINkytW3INbUINaAhDuZ7GKm_4DSSKeHSpdm3K-TPQrhNPn5B-21wN24ZH8flimqC7ikTzmRrYZBAsyD_JSC8YW8BDJZwmTby9R7LZC9SYkyHkSs7NPVZxXiqEDqjhkRJNxZwqqyZaVgYjISiFpzXsSyGZmL1YFCQ/s220/WhatsApp%20Image%202024-05-17%20at%2017.42.05.jpeg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjXVVzgWET0q3t-LtOwrLVs0v5yM0cmqhDLMIQIR7vxkYGP9PzwDvwEHuK1rZuqjvf1BsLOmH9CntLnVwJWxaF_bIV13bEhPnwJ_FtZTrGqymvmKi1ZcE_Wo5ia-iFt1nBMTQM33VCPRiylKV47PNvEfNUxg5XbgbYWFCeVs7_NHQJZvZNqXeq/s72-w640-h402-c/rede-de-mensagens-de-conexao-de-comunicacao.jpg" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-33774862.post-8745470977471584516</id><published>2025-11-08T18:32:00.007-03:00</published><updated>2025-11-26T12:03:12.451-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="automação"/><category scheme="http://www.blogger.com/atom/ns#" term="finanças pessoais"/><category scheme="http://www.blogger.com/atom/ns#" term="produtividade"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><title type='text'>5 Automações Simples para Economizar Tempo e Dinheiro usando Python e APIs</title><content type='html'>&lt;div class=&quot;post-header&quot;&gt;
  &lt;h1&gt;5 Automações Simples para Economizar Tempo e Dinheiro usando Python e APIs&lt;/h1&gt;
  &lt;p class=&quot;post-meta&quot;&gt;
    Tempo de leitura: 10 minutos
  &lt;/p&gt;
&lt;/div&gt;
&lt;h2 data-end=&quot;301&quot; data-start=&quot;286&quot;&gt;Introdução&lt;/h2&gt;
&lt;p data-end=&quot;624&quot; data-start=&quot;302&quot;&gt;Você gasta horas repetindo tarefas manuais que poderiam executar sozinhas? Então vejamos como usar Python + APIs para automatizar processos e poupar tempo &lt;span data-end=&quot;461&quot; data-start=&quot;456&quot;&gt;e&lt;/span&gt; dinheiro. Neste artigo você vai aprender 5 scripts práticos e fáceis de implementar — perfeitos para quem não é engenheiro de software, mas quer resultados reais.&lt;/p&gt;&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixZDpIhT224Xu6SbjYGhtAQNJ4zXK9Q_vpBjQFAEbEctEyhlKjcU5Ao4VH4aeSb5sSyd34B6afqkEXaApLQmiQApTScPmPepQLKYGJgBV7uMvkF1DF-Vd9sGmjRn6zvYAD1AipyGhLM-4KBGrZPFeT5BGCW9A1-7V6YMV0wlHUfjjN86IE47nV/s626/ampulheta-de-vidro-e-cofrinho_218381-14620.jpg&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;417&quot; data-original-width=&quot;626&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixZDpIhT224Xu6SbjYGhtAQNJ4zXK9Q_vpBjQFAEbEctEyhlKjcU5Ao4VH4aeSb5sSyd34B6afqkEXaApLQmiQApTScPmPepQLKYGJgBV7uMvkF1DF-Vd9sGmjRn6zvYAD1AipyGhLM-4KBGrZPFeT5BGCW9A1-7V6YMV0wlHUfjjN86IE47nV/s16000/ampulheta-de-vidro-e-cofrinho_218381-14620.jpg&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Fonte:&amp;nbsp;https://br.freepik.com/&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;h2 data-end=&quot;700&quot; data-start=&quot;631&quot;&gt;1. Automação #1: Cotação do dólar (ou outra moeda) em tempo real&lt;/h2&gt;
&lt;h3 data-end=&quot;727&quot; data-start=&quot;701&quot;&gt;Por que isso importa&lt;/h3&gt;
&lt;p data-end=&quot;874&quot; data-start=&quot;728&quot;&gt;Se você faz negócios internacionais, ou você trabalha com importações, vendas ou orçamentos em moeda estrangeira, saber a cotação atual de uma moeda pode evitar surpresas.&lt;/p&gt;
&lt;h3 data-end=&quot;891&quot; data-start=&quot;875&quot;&gt;Como fazer&lt;/h3&gt;
&lt;ol data-end=&quot;1154&quot; data-start=&quot;892&quot;&gt;
&lt;li data-end=&quot;976&quot; data-start=&quot;892&quot;&gt;
&lt;p data-end=&quot;976&quot; data-start=&quot;895&quot;&gt;Use uma API de câmbio, por exemplo uma que retorne “USD/BRL” ou outra paridade. Iremos usar &lt;a href=&quot;https://awesomeapi.com.br/&quot; target=&quot;_blank&quot;&gt;AwesomeAPI&lt;/a&gt;, que permite até 10 mil requisições mensais gratuitas.&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;1065&quot; data-start=&quot;977&quot;&gt;
&lt;p data-end=&quot;1065&quot; data-start=&quot;980&quot;&gt;Em Python, use &lt;code data-end=&quot;1011&quot; data-start=&quot;995&quot;&gt;requests.get()&lt;/code&gt; para chamar a API, parse o &lt;code data-end=&quot;1045&quot; data-start=&quot;1039&quot;&gt;json&lt;/code&gt; e extraia a taxa.&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;1154&quot; data-start=&quot;1066&quot;&gt;
&lt;p data-end=&quot;1154&quot; data-start=&quot;1069&quot;&gt;Armazene ou mostre a taxa num relatório ou envie uma notificação (e-mail ou Telegram). No próximo post iremos mostrar como enviar notificações pelo Gmail e pelo Telegram.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 data-end=&quot;1200&quot; data-start=&quot;1155&quot;&gt;Esboço de script (exemplo simplificado)&lt;/h3&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; requests&lt;/span&gt;


&lt;span style=&quot;color: #f8f8f2;&quot;&gt;LIMITE_SUPERIOR&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ffb86c;&quot;&gt;5.4&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;LIMITE_INFERIOR&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ffb86c;&quot;&gt;5.0&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;monitorar_cotacao&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;():&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;API_URL&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;https://economia.awesomeapi.com.br/last/USD-BRL&quot;&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;resp&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;requests&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;get(API_URL)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;data&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;resp&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;json()&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;cotacao_dolar&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(data[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;USDBRL&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;][&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;bid&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;])&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;cotacao_dolar&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;__name__&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;cotacao_atual&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;monitorar_cotacao()&lt;/span&gt;

    &lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;cotacao_atual&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;LIMITE_SUPERIOR:&lt;/span&gt;
        &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Alerta de Cotação:&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;A cotação do dólar subiu para {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;cotacao_atual&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;:.2f} BRL.&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;elif&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;cotacao_atual&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;LIMITE_INFERIOR:&lt;/span&gt;
        &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Alerta de Cotação&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;A cotação do dólar caiu para {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;cotacao_atual&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;:.2f} BRL.&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;h3 data-end=&quot;1500&quot; data-start=&quot;1474&quot;&gt;Dica para economizar&lt;/h3&gt;
&lt;p data-end=&quot;1688&quot; data-start=&quot;1501&quot;&gt;Agende esse script para rodar uma vez por dia (usando a biblioteca &lt;i&gt;schedule&lt;/i&gt; do Python). Se a taxa ultrapassar um valor crítico (ex: R$ 5,40), você recebe um alerta e pode agir antes de fechar negócio.&lt;/p&gt;
&lt;hr data-end=&quot;1693&quot; data-start=&quot;1690&quot; /&gt;
&lt;h2 data-end=&quot;1757&quot; data-start=&quot;1695&quot;&gt;2. Automação #2: Alerta de gastos ou despesas inesperadas&lt;/h2&gt;
&lt;h3 data-end=&quot;1784&quot; data-start=&quot;1758&quot;&gt;Por que isso importa&lt;/h3&gt;
&lt;p data-end=&quot;1965&quot; data-start=&quot;1785&quot;&gt;Muitas pessoas não revisam seus gastos com frequência — e uma despesa inesperada pode passar batida ou essa pessoa pode ser surpreendida por um estouro no orçamento. Uma automação simples pode alertar você antes que isso se torne um problema.&lt;/p&gt;
&lt;h3 data-end=&quot;1982&quot; data-start=&quot;1966&quot;&gt;Como fazer&lt;/h3&gt;
&lt;ol data-end=&quot;2242&quot; data-start=&quot;1983&quot;&gt;
&lt;li data-end=&quot;2071&quot; data-start=&quot;1983&quot;&gt;
&lt;p data-end=&quot;2071&quot; data-start=&quot;1986&quot;&gt;Conecte-se à planilha que mostramos no &lt;a href=&quot;https://professorcarlos.blogspot.com/2025/11/como-automatizar-suas-financas-pessoais.html&quot;&gt;post anterior&lt;/a&gt;, onde foi apresentado passo a passo como automatizar suas finanças.&lt;/p&gt;&lt;/li&gt;

&lt;li data-end=&quot;2171&quot; data-start=&quot;2072&quot;&gt;
&lt;p data-end=&quot;2171&quot; data-start=&quot;2075&quot;&gt;Em Python, filtre transações no período desejado, acima de um valor definido, de determinada categoria ou mesmo o valor total das transações.&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;2242&quot; data-start=&quot;2172&quot;&gt;
&lt;p data-end=&quot;2242&quot; data-start=&quot;2175&quot;&gt;Se encontrar valores que extrapolem os limites estabelecidos por você, envie e-mail ou mensagem com aviso.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 data-end=&quot;2265&quot; data-start=&quot;2243&quot;&gt;Esboço de script&lt;/h3&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px; text-align: left;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; pandas &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; pd&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; automatizar_financas &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;conectar_google_sheets&lt;/span&gt;


&lt;span style=&quot;color: #f8f8f2;&quot;&gt;MES_ATUAL&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;10&quot;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;ORCAMENTO_MENSAL&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ffb86c;&quot;&gt;2000.0&lt;/span&gt;  &lt;span style=&quot;color: #6272a4;&quot;&gt;# Exemplo de orçamento mensal&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;calcular_despesas&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;():&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;client&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;conectar_google_sheets()&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;planilha&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;client&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;open(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Minhas Finanças Pessoais&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;worksheet&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;planilha&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;worksheet(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Transações&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;

    &lt;span style=&quot;color: #6272a4;&quot;&gt;# Obter dados atualizados&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;transacoes&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;worksheet&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;get_all_records()&lt;/span&gt;
    &lt;span style=&quot;color: #6272a4;&quot;&gt;# Converter para DataFrame do pandas&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;transacoes_df&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;pd&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;DataFrame(transacoes)&lt;/span&gt;
    &lt;span style=&quot;color: #6272a4;&quot;&gt;# Filtrar apenas despesas do mês atual&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;despesas&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;transacoes_df[&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;(transacoes_df[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Tipo&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Despesa&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
        &lt;span style=&quot;color: #ff79c6;&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;(transacoes_df[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Data&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;str&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;contains(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;/{&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;MES_ATUAL&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}/&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;))&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;total_despesas&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;despesas[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Valor&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;sum()&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;total_despesas&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;__name__&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;total_despesas&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;calcular_despesas()&lt;/span&gt;
    &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;Total de despesas no mês atual: R$ {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;total_despesas&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;:.2f}&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;total_despesas&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;ORCAMENTO_MENSAL:&lt;/span&gt;
        &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Atenção: Você ultrapassou o orçamento mensal!&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
        &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Você está dentro do orçamento mensal.&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Antes de tudo instale o &lt;i&gt;pandas&lt;/i&gt; com:&lt;/p&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;pip&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;install&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;pandas&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Observe que reutilizamos a função&amp;nbsp;&lt;i&gt;conectar_google_sheets()&lt;/i&gt;, que conecta à planilha criada no post anterior. Daí lemos os registros, fazemos a conversão da lista para um DataFrame, o que vai facilitar a filtragem dos dados desejados e a própria totalização.&lt;/p&gt;&lt;h3 data-end=&quot;2711&quot; data-start=&quot;2685&quot;&gt;Dica para economizar&lt;/h3&gt;
&lt;p data-end=&quot;2891&quot; data-start=&quot;2712&quot;&gt;Use isso para categorizar “gastos recorrentes + variações”. Se algo aumentou 30%, por exemplo, sobre o mês anterior, você já recebe o alerta e pode renegociar ou cancelar antes que vire padrão.&lt;/p&gt;
&lt;hr data-end=&quot;2896&quot; data-start=&quot;2893&quot; /&gt;
&lt;h2 data-end=&quot;2987&quot; data-start=&quot;2898&quot;&gt;3. Automação #3: Monitor de preço para compra de um produto&lt;/h2&gt;
&lt;h3 data-end=&quot;3014&quot; data-start=&quot;2988&quot;&gt;Por que isso importa&lt;/h3&gt;
&lt;p data-end=&quot;3208&quot; data-start=&quot;3015&quot;&gt;Suponha que você deseja adquirir um produto, mas está aguardando um desconto, uma promoção que leve o preço desse produto a um valor viável para você. Ficar na frente da tela aguardando isso acontecer não é uma opção. Uma automação que faça esse trabalho e lhe envie um alerta seria uma mão na roda.&lt;/p&gt;
&lt;h3 data-end=&quot;3225&quot; data-start=&quot;3209&quot;&gt;Como fazer&lt;/h3&gt;
&lt;ol data-end=&quot;3478&quot; data-start=&quot;3226&quot;&gt;
&lt;li data-end=&quot;3317&quot; data-start=&quot;3226&quot;&gt;
&lt;p data-end=&quot;3317&quot; data-start=&quot;3229&quot;&gt;Identifique o endpoint ou API que fornece o preço ou use &lt;i&gt;scraping&lt;/i&gt; (se permitido no site alvo).&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;3369&quot; data-start=&quot;3318&quot;&gt;
&lt;p data-end=&quot;3369&quot; data-start=&quot;3321&quot;&gt;Em Python, periodicamente consulte esse preço.&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;3478&quot; data-start=&quot;3370&quot;&gt;
&lt;p data-end=&quot;3478&quot; data-start=&quot;3373&quot;&gt;Se o valor cair abaixo de um limiar que você definiu, envie alerta para aproveitar a oferta ou negocie.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 data-end=&quot;3501&quot; data-start=&quot;3479&quot;&gt;Esboço de script&lt;/h3&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px; text-align: left;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; bs4 &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;BeautifulSoup&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; requests&lt;/span&gt;


&lt;span style=&quot;color: #f8f8f2;&quot;&gt;PRECO_ALVO&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #ffb86c;&quot;&gt;200.0&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;monitorar_preco&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(url):&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;headers&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;User-Agent&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3&quot;&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;response&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;requests&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;get(url,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;headers&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;headers)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;page_contents&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;response&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;text&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;doc&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;BeautifulSoup(page_contents,&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;html.parser&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;price&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;doc&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;select_one(&lt;/span&gt;
      &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;div.ui-pdp-price__second-line span.andes-money-amount__fraction&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;price_cents&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;doc&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;select_one(&lt;/span&gt;
      &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;div.ui-pdp-price__second-line span.andes-money-amount__cents&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;final_price&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;{&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;price&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}.{&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;price_cents&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&quot;&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(final_price)&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;__name__&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;url&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;https://www.mercadolivre.com.br/parafusadeira-furadeira-de-impacto-the-black-tools-profissional-tb-21px-2-baterias-com-maleta-60hz-amarelo/p/MLB30144703#polycard_client=offers&amp;amp;deal_print_id=3f44d512-e52a-4a96-80e5-fb460eadceb4&amp;amp;position=7&amp;amp;tracking_id=01757776-3524-4dab-a168-16eb7c0c96d0&amp;amp;wid=MLB4268042659&amp;amp;sid=offers&quot;&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;preco_atual&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;monitorar_preco(url)&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;preco_atual&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;PRECO_ALVO:&lt;/span&gt;
        &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Alerta de Preço:&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;O preço do produto caiu para R$ {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;preco_atual&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;:.2f}!&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Primeiramente instale o BeautifulSoup com:&lt;/div&gt;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;pip&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;install&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;beautifulsoup4&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;

Neste exemplo utilizamos o link de um produto no Mercado Livre. A função &lt;i&gt;monitorar_preco()&lt;/i&gt; só funciona no Mercado Livre, dado que cada site tem suas peculiaridades, nomes de classes, &lt;i&gt;tag&lt;/i&gt; onde se localiza o preço, etc&lt;/div&gt;&lt;h3 data-end=&quot;3817&quot; data-start=&quot;3791&quot;&gt;Dica para economizar&lt;/h3&gt;
&lt;p data-end=&quot;3981&quot; data-start=&quot;3818&quot;&gt;Combine esse script com envio de mensagem via Telegram ou e-mail. Assim você pode receber uma mensagem “isso está barato agora” e decidir com calma a aquisição.&lt;/p&gt;
&lt;hr data-end=&quot;3986&quot; data-start=&quot;3983&quot; /&gt;
&lt;h2 data-end=&quot;4060&quot; data-start=&quot;3988&quot;&gt;4. Automação #4: Monitor de concursos públicos para devs&lt;/h2&gt;
&lt;h3 data-end=&quot;4087&quot; data-start=&quot;4061&quot;&gt;Por que isso importa&lt;/h3&gt;
&lt;p data-end=&quot;4293&quot; data-start=&quot;4088&quot;&gt;Suponha o cenário em que você está batalhando por uma vaga de trabalho e os concursos públicos podem ser uma oportunidade. Se você pudesse receber uma notificação no seu e-mail ou mesmo no Telegram pelo smartphone seria de grande ajuda. Assim você estaria em dia com as publicações de editais.&lt;/p&gt;
&lt;h3 data-end=&quot;4310&quot; data-start=&quot;4294&quot;&gt;Como fazer&lt;/h3&gt;
&lt;ol data-end=&quot;4599&quot; data-start=&quot;4311&quot;&gt;
&lt;li data-end=&quot;4419&quot; data-start=&quot;4311&quot;&gt;
&lt;p data-end=&quot;4419&quot; data-start=&quot;4314&quot;&gt;Uma solução é fazer &lt;i&gt;scraping&lt;/i&gt; de sites agregadores de editais. Aqui escolhemos o &lt;a href=&quot;https://www.pciconcursos.com.br/&quot; target=&quot;_blank&quot;&gt;PCI Concursos&lt;/a&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;4527&quot; data-start=&quot;4420&quot;&gt;
&lt;p data-end=&quot;4527&quot; data-start=&quot;4423&quot;&gt;Em Python, monitore regularmente os anúncios em busca de palavras-chaves como &lt;i&gt;Engenheiro de Software&lt;/i&gt;, &lt;i&gt;Desenvolvedor&lt;/i&gt;, etc.&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;4599&quot; data-start=&quot;4528&quot;&gt;
&lt;p data-end=&quot;4599&quot; data-start=&quot;4531&quot;&gt;Crie um alerta por e-mail ou Telegram quando o script encontrar alguma(s) de suas palavras-chaves alvo.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 data-end=&quot;4622&quot; data-start=&quot;4600&quot;&gt;Esboço de script&lt;/h3&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; requests&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; bs4 &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;BeautifulSoup&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;verificar_concursos&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(palavras_chave&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;[]):&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;url&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;https://www.pciconcursos.com.br/&quot;&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;headers&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;User-Agent&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3&quot;&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;}&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;response&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;requests&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;get(url,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;headers&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;headers)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;page_contents&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;BeautifulSoup(response&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;text,&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;html.parser&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;

    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;noticias&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;page_contents&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;find(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;div&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;noticias_capa&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #6272a4;&quot;&gt;# Procure por palavras-chaves na página&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;exist&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;any&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;
      &lt;span style=&quot;color: #f8f8f2;&quot;&gt;elemento&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;noticias&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;get_text()&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;lower()&lt;/span&gt;
      &lt;span style=&quot;color: #ff79c6;&quot;&gt;for&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;elemento&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;palavras_chave)&lt;/span&gt;

    &lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;exist&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;__name__&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;verificar_concursos(&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;[&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;desenvolvedor&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;programador&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;engenheiro de software&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
            &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;tecnologia da informação&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;):&lt;/span&gt;
        &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Novo concurso encontrado!&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;h3 data-end=&quot;4984&quot; data-start=&quot;4958&quot;&gt;Dica para economizar&lt;/h3&gt;
&lt;p data-end=&quot;5163&quot; data-start=&quot;4985&quot;&gt;Inclua outros sites similares ao PCI Concursos, adicione outros nomes de vagas na lista e agende esse script para rodar a cada 30 dias e, sempre que ele retornar positivo, envie uma notificação.&lt;/p&gt;
&lt;hr data-end=&quot;5168&quot; data-start=&quot;5165&quot; /&gt;
&lt;h2 data-end=&quot;5227&quot; data-start=&quot;5170&quot;&gt;5. Automação #5: Backup automático de dados críticos&lt;/h2&gt;
&lt;h3 data-end=&quot;5254&quot; data-start=&quot;5228&quot;&gt;Por que isso importa&lt;/h3&gt;
&lt;p data-end=&quot;5453&quot; data-start=&quot;5255&quot;&gt;Perder dados de clientes, documentos, planilhas ou dashboards pode significar retrabalho ou até risco de perder dinheiro. Fazer backups manuais dá trabalho e pode falhar, principalmente por esquecimento. Automatizar essa tarefa pode ser uma garantia de segurança.&lt;/p&gt;
&lt;h3 data-end=&quot;5470&quot; data-start=&quot;5454&quot;&gt;Como fazer&lt;/h3&gt;
&lt;ol data-end=&quot;5789&quot; data-start=&quot;5471&quot;&gt;
&lt;li data-end=&quot;5570&quot; data-start=&quot;5471&quot;&gt;
&lt;p data-end=&quot;5570&quot; data-start=&quot;5474&quot;&gt;Use API de armazenamento em nuvem (por exemplo: Google Drive API, Dropbox API) ou conexão FTP.&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;5649&quot; data-start=&quot;5571&quot;&gt;
&lt;p data-end=&quot;5649&quot; data-start=&quot;5574&quot;&gt;Em Python, selecione os arquivos/diretórios críticos no horário definido.&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;5789&quot; data-start=&quot;5650&quot;&gt;
&lt;p data-end=&quot;5789&quot; data-start=&quot;5653&quot;&gt;Faça upload para o serviço de nuvem ou outro local seguro. Opcional: mantenha versionamento ou apague backups antigos automaticamente.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 data-end=&quot;5812&quot; data-start=&quot;5790&quot;&gt;Esboço de script&lt;/h3&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; text-align: left; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; zipfile &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;ZipFile,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;ZIP_DEFLATED&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; dropbox&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; pathlib&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; os&lt;/span&gt;


&lt;span style=&quot;color: #f8f8f2;&quot;&gt;TOKEN&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;seu access_token aqui&#39;&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;criar_backup&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(diretorio_arquivo,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;arquivo_backup):&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;with&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;ZipFile(arquivo_backup,&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;w&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;ZIP_DEFLATED)&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;zipf:&lt;/span&gt;
        &lt;span style=&quot;color: #ff79c6;&quot;&gt;for&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;pasta_atual,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;subpastas,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;arquivos&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;os&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;walk(diretorio_arquivo):&lt;/span&gt;
            &lt;span style=&quot;color: #ff79c6;&quot;&gt;for&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;arquivo&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;arquivos:&lt;/span&gt;
                &lt;span style=&quot;color: #f8f8f2;&quot;&gt;caminho_completo&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;os&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;path&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;join(pasta_atual,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;arquivo)&lt;/span&gt;
                &lt;span style=&quot;color: #f8f8f2;&quot;&gt;zipf&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;write(caminho_completo,&lt;/span&gt;
                           &lt;span style=&quot;color: #f8f8f2;&quot;&gt;os&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;path&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;relpath(&lt;/span&gt;
                             &lt;span style=&quot;color: #f8f8f2;&quot;&gt;caminho_completo,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;diretorio_arquivo))&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;dropbox_backup&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(arquivo_backup,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;token_acesso):&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;local_filepath&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;pathlib&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Path(arquivo_backup)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;dropbox_path&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;f&#39;/{&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;arquivo_backup&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&#39;&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;dbx&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;dropbox&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Dropbox(token_acesso)&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;with&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;local_filepath&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;open(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;rb&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;f:&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;dbx&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;files_upload(&lt;/span&gt;
          &lt;span style=&quot;color: #f8f8f2;&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;read(),&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;dropbox_path,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;mode&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;dropbox&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;files&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;WriteMode(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;overwrite&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;))&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;__name__&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;criar_backup(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;../cinco-scripts&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;backup.zip&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;dropbox_backup(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;backup.zip&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;TOKEN)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;os&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;remove(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;backup.zip&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Backup criado e enviado para o Dropbox com sucesso.&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;Neste script precisamos instalar, antes de tudo, o pacote &lt;i&gt;dropbox&lt;/i&gt;:&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;pip&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;install&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;dropbox&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style=&quot;text-align: left;&quot;&gt;A função &lt;i&gt;criar_backup()&lt;/i&gt; cria um arquivo compactado da pasta que desejamos salvar, usando o pacote &lt;i&gt;zipfile&lt;/i&gt;. A função &lt;i&gt;dropbox_backup()&lt;/i&gt; faz o upload do arquivo compactado para nossa conta Dropbox.&amp;nbsp;&lt;/div&gt;&lt;h3 data-end=&quot;6214&quot; data-start=&quot;6188&quot;&gt;Dica para economizar&lt;/h3&gt;
&lt;p data-end=&quot;6365&quot; data-start=&quot;6215&quot;&gt;Obs: os protocolos de segurança de armazenamento em nuvem tem mudado e o Dropbox não permite mais gerar token que não expira. Então essa automação fica dependendo do usuário para obter o token. Portanto, pode-se escolher entre fazer o backup manualmente ou obter o token.&lt;/p&gt;&lt;p data-end=&quot;6365&quot; data-start=&quot;6215&quot;&gt;Uma sugestão seria agendar o envio de uma mensagem no Telegram alertando-o para fazer o backup e agendar para um tempinho depois&amp;nbsp; execução do backup. Isso evitaria “descobrir” só depois que perdeu algo valioso.&lt;/p&gt;
&lt;hr data-end=&quot;6370&quot; data-start=&quot;6367&quot; /&gt;
&lt;h2 data-end=&quot;6437&quot; data-start=&quot;6372&quot;&gt;Como adaptar isso para você&lt;/h2&gt;
&lt;ul data-end=&quot;6997&quot; data-start=&quot;6438&quot;&gt;
&lt;li data-end=&quot;6504&quot; data-start=&quot;6438&quot;&gt;
&lt;p data-end=&quot;6504&quot; data-start=&quot;6440&quot;&gt;Escolha 1 automação para começar — não tente todas de uma vez.&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;6610&quot; data-start=&quot;6505&quot;&gt;
&lt;p data-end=&quot;6610&quot; data-start=&quot;6507&quot;&gt;Use bibliotecas Python simples como &lt;code data-end=&quot;6553&quot; data-start=&quot;6543&quot;&gt;&lt;i&gt;requests&lt;/i&gt;&lt;/code&gt;, &lt;code data-end=&quot;6563&quot; data-start=&quot;6555&quot;&gt;&lt;i&gt;pandas&lt;/i&gt;&lt;/code&gt; e &lt;code data-end=&quot;6576&quot; data-start=&quot;6566&quot;&gt;&lt;i&gt;schedule&lt;/i&gt;&lt;/code&gt;&amp;nbsp;para agendar tarefas.&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;6724&quot; data-start=&quot;6611&quot;&gt;
&lt;p data-end=&quot;6724&quot; data-start=&quot;6613&quot;&gt;Documente seu “workflow automático” para que, se você terceirizar ou vender isso como serviço, esteja pronto.&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;6858&quot; data-start=&quot;6725&quot;&gt;
&lt;p data-end=&quot;6858&quot; data-start=&quot;6727&quot;&gt;Defina alertas claros (SMS, Telegram, e-mail) para que a automação não fique apenas “rodando” mas &lt;strong data-end=&quot;6855&quot; data-start=&quot;6832&quot;&gt;entregue valor real&lt;/strong&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li data-end=&quot;6997&quot; data-start=&quot;6859&quot;&gt;
&lt;p data-end=&quot;6997&quot; data-start=&quot;6861&quot;&gt;Meça o tempo/dinheiro que cada automação te economiza. Por exemplo: “economizei 1 h/mês = R$ X” — isso te ajuda a justificar ou escalar.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr data-end=&quot;7002&quot; data-start=&quot;6999&quot; /&gt;
&lt;h2 data-end=&quot;7018&quot; data-start=&quot;7004&quot;&gt;Conclusão&lt;/h2&gt;
&lt;p data-end=&quot;7438&quot; data-start=&quot;7019&quot;&gt;Automatizar com Python + APIs não exige ser um programador sênior. Com cinco scripts simples — cotação de moeda, alerta de gastos, monitor de preços, monitor de concursos e backup de dados — você já poderá &lt;strong data-end=&quot;7278&quot; data-start=&quot;7227&quot;&gt;economizar horas, evitar erros e reduzir custos&lt;/strong&gt;.&lt;br data-end=&quot;7282&quot; data-start=&quot;7279&quot; /&gt;
Comece pequeno, escale conforme o conforto cresce, e logo você estará mais focado no que importa (criar, vender, crescer) em vez de repetir tarefas manuais.&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://professorcarlos.blogspot.com/feeds/8745470977471584516/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/33774862/8745470977471584516?isPopup=true' title='0 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/8745470977471584516'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/8745470977471584516'/><link rel='alternate' type='text/html' href='http://professorcarlos.blogspot.com/2025/11/5-automacoes-simples-para-economizar-tempo-e-dinheiro-usando-Python-e-APIs.html' title='5 Automações Simples para Economizar Tempo e Dinheiro usando Python e APIs'/><author><name>Professor Carlos</name><uri>http://www.blogger.com/profile/10347668022241306511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFBZD8558AE67R-CINkytW3INbUINaAhDuZ7GKm_4DSSKeHSpdm3K-TPQrhNPn5B-21wN24ZH8flimqC7ikTzmRrYZBAsyD_JSC8YW8BDJZwmTby9R7LZC9SYkyHkSs7NPVZxXiqEDqjhkRJNxZwqqyZaVgYjISiFpzXsSyGZmL1YFCQ/s220/WhatsApp%20Image%202024-05-17%20at%2017.42.05.jpeg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixZDpIhT224Xu6SbjYGhtAQNJ4zXK9Q_vpBjQFAEbEctEyhlKjcU5Ao4VH4aeSb5sSyd34B6afqkEXaApLQmiQApTScPmPepQLKYGJgBV7uMvkF1DF-Vd9sGmjRn6zvYAD1AipyGhLM-4KBGrZPFeT5BGCW9A1-7V6YMV0wlHUfjjN86IE47nV/s72-c/ampulheta-de-vidro-e-cofrinho_218381-14620.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-33774862.post-4484556500032792671</id><published>2025-11-03T09:40:00.007-03:00</published><updated>2025-12-13T18:42:07.905-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="automação"/><category scheme="http://www.blogger.com/atom/ns#" term="finanças pessoais"/><category scheme="http://www.blogger.com/atom/ns#" term="google sheets"/><category scheme="http://www.blogger.com/atom/ns#" term="python"/><title type='text'>Como Automatizar suas Finanças Pessoais com Google Sheets e Python</title><content type='html'>&lt;div class=&quot;post-header&quot;&gt;
  &lt;h1&gt;Como Automatizar suas Finanças Pessoais com Google Sheets e Python&lt;/h1&gt;
  &lt;p class=&quot;post-meta&quot;&gt;
    Tempo de leitura: 8 minutos
  &lt;/p&gt;
&lt;/div&gt;

&lt;div class=&quot;post-content&quot;&gt;
  &lt;p&gt;&lt;i&gt;Obs: Este é o primeiro artigo da nova fase do blog, agora focado em finanças e automação para desenvolvedores.&lt;/i&gt;&lt;/p&gt;&lt;p&gt;
    Manter o controle das finanças pessoais pode ser uma tarefa tediosa e
    repetitiva. Que tal automatizar uma parte ou até todo o processo, tendo suas
    receitas, investimentos e gastos atualizados automaticamente? Neste guia
    prático, iremos mostrar como criar uma planilha atualizada automaticamente
    usando Google Sheets e Python. O tutorial apresenta uma planilha básica que
    você poderá melhorar à medida que ganha experiência.
  &lt;/p&gt;&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhBg1_OMAV98BXXAmWoDibCFBI1mF8SRjsdTORbJf_imp2NLu8a9ETrkXA6fY7X_rwp5b8s3jK3NfZi20UBOlNFnd6OrVmHbdSXLhvSkdXL-sk1-5y6YHsiXnBWF89YEQOxyHa_SfuX5DmuKCl_eM5iVtxQSMJ1z1RSmWVGADRkYoW_N5uP5rEn/s5000/analise-de-estrategia-de-estudo-de-informacao-de-planejamento-de-negocios.jpg&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;3337&quot; data-original-width=&quot;5000&quot; height=&quot;429&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhBg1_OMAV98BXXAmWoDibCFBI1mF8SRjsdTORbJf_imp2NLu8a9ETrkXA6fY7X_rwp5b8s3jK3NfZi20UBOlNFnd6OrVmHbdSXLhvSkdXL-sk1-5y6YHsiXnBWF89YEQOxyHa_SfuX5DmuKCl_eM5iVtxQSMJ1z1RSmWVGADRkYoW_N5uP5rEn/w640-h429/analise-de-estrategia-de-estudo-de-informacao-de-planejamento-de-negocios.jpg&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Fonte:&amp;nbsp;&lt;span face=&quot;Roboto, Ubuntu, Arial, sans-serif&quot; style=&quot;background-color: white; color: #1f1f1f; font-size: 13px; text-align: start; text-wrap-mode: nowrap;&quot;&gt;https://br.freepik.com&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;&lt;strong&gt;💡 Dica:&lt;/strong&gt; Este guia assume que você tem conhecimentos
    básicos de Python e planilhas. Mesmo sem experiência avançada, você
    conseguirá seguir os passos!&lt;/p&gt;

  &lt;h2&gt;Por que Automatizar suas Finanças?&lt;/h2&gt;

  &lt;ul&gt;
    &lt;li&gt;
      &lt;strong&gt;Economia de tempo:&lt;/strong&gt; Chega de digitar extrato por extrato
    &lt;/li&gt;
    &lt;li&gt;&lt;strong&gt;Visão em tempo real:&lt;/strong&gt; Dados sempre atualizados&lt;/li&gt;
    &lt;li&gt;
      &lt;strong&gt;Tomada de decisão:&lt;/strong&gt; Informações precisas para investir
      melhor
    &lt;/li&gt;
    &lt;li&gt;
      &lt;strong&gt;Controle de gastos:&lt;/strong&gt; Alertas automáticos quando
      ultrapassar orçamentos
    &lt;/li&gt;
  &lt;/ul&gt;

  &lt;h2&gt;Pré-requisitos&lt;/h2&gt;

  &lt;ul&gt;
    &lt;li&gt;Conta no Google e credenciais no Google Cloud&lt;/li&gt;
    &lt;li&gt;Python e bibliotecas instaladas&lt;/li&gt;
    &lt;li&gt;Conhecimento básico de planilhas e programação&lt;/li&gt;
  &lt;/ul&gt;

  &lt;h2&gt;Passo a Passo da Automação&lt;/h2&gt;

  &lt;h3&gt;1. Estruturando sua Planilha no Google Sheets&lt;/h3&gt;

  &lt;p&gt;Crie uma planilha com as seguintes abas:&lt;/p&gt;

  &lt;p&gt;&lt;strong&gt;📊 Dashboard Principal&lt;/strong&gt;&lt;/p&gt;
  &lt;div class=&quot;code-block&quot;&gt;
Saldo Atual: R$&lt;br /&gt;
Receita do Mês: R$&lt;br /&gt;
Despesas do Mês: R$&lt;br /&gt;
Investimentos: R$  &lt;/div&gt;

  &lt;p&gt;&lt;strong&gt;💳 Transações &lt;/strong&gt;(com as seguintes colunas)&lt;/p&gt;
  &lt;table&gt;
    &lt;thead&gt;
      &lt;tr&gt;
        &lt;th&gt;Data&lt;/th&gt;
        &lt;th&gt;Descrição&lt;/th&gt;
        &lt;th&gt;Categoria&lt;/th&gt;
        &lt;th&gt;Valor&lt;/th&gt;
        &lt;th&gt;Tipo&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr&gt;
        &lt;td&gt;&lt;/td&gt;
        &lt;td&gt;&lt;/td&gt;
        &lt;td&gt;&lt;/td&gt;
        &lt;td&gt;&lt;/td&gt;
        &lt;td&gt;&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;

  &lt;p&gt;&lt;strong&gt;📈 Investimentos &lt;/strong&gt;(com as seguintes colunas)&lt;/p&gt;
  &lt;table&gt;
    &lt;thead&gt;
      &lt;tr&gt;
        &lt;th&gt;Ativo&lt;/th&gt;
        &lt;th&gt;Quantidade&lt;/th&gt;
        &lt;th&gt;Preço Médio&lt;/th&gt;
        &lt;th&gt;Preço Atual&lt;/th&gt;
        &lt;th&gt;Valor Investido&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr&gt;
        &lt;td&gt;&lt;/td&gt;
        &lt;td&gt;&lt;/td&gt;
        &lt;td&gt;&lt;/td&gt;
        &lt;td&gt;&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;
  &lt;h3&gt;2. Ativar APIs do Google Drive e Google Sheets e obter credenciais&lt;/h3&gt;
  &lt;p&gt;O primeiro passo aqui, após logar na sua conta Google, consiste em acessar o &lt;a href=&quot;https://cloud.google.com/&quot; target=&quot;_blank&quot;&gt;Google Cloud&lt;/a&gt; e clicar em &lt;b&gt;Console&lt;/b&gt; no canto superior direito.&lt;/p&gt;&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;border: 1px solid rgb(0, 0, 0); text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEheQ7QumV-kqg7rsSNqZwWnZQGHygbXCJSAECGyQ-r9uJED4SX4BADtyF1CdJU1n3RdM3n3oIvYp3TjPAiSbo3JgKivS0TjPaLbGPkSP1ZOyE_s76h4qkBLtgIryk-p2R-ozRRZNCUPl9GQ167w5bIwGenu1GtzSVH325hL-Q_f7Y8Iv-8A9Pc7/s1321/console.jpg&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;202&quot; data-original-width=&quot;1321&quot; height=&quot;98&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEheQ7QumV-kqg7rsSNqZwWnZQGHygbXCJSAECGyQ-r9uJED4SX4BADtyF1CdJU1n3RdM3n3oIvYp3TjPAiSbo3JgKivS0TjPaLbGPkSP1ZOyE_s76h4qkBLtgIryk-p2R-ozRRZNCUPl9GQ167w5bIwGenu1GtzSVH325hL-Q_f7Y8Iv-8A9Pc7/w640-h98/console.jpg&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Botão Console no Google Cloud&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;p&gt;Em seguida clique no menu e na opção &lt;b&gt;APIs e serviços&lt;/b&gt; ou use os botões de acesso rápido. Clique então em &lt;b&gt;Biblioteca&lt;/b&gt; no menu e ative &lt;b&gt;Google Drive API&lt;/b&gt; e &lt;b&gt;Google Sheets API&lt;/b&gt;.&lt;/p&gt;&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;border: 1px solid rgb(0, 0, 0); text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjO3B2DmH8oN3kau2iivebtvUTjdxqcQPbM4ph3kVvZ6vDZxk6SfWXnIyR4xakwNn8_wZApsZDE3dYy8yD71tOZyaKx0g7SfLdJ1VbEaDUArFf9wPGaubQj-27QHqGVwXHD5bSfIejeQWayC6QL5Gnxrebj_qG040yEv6sN_VTHYf1p1c-9dP5M/s1112/menu.jpg&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;547&quot; data-original-width=&quot;1112&quot; height=&quot;315&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjO3B2DmH8oN3kau2iivebtvUTjdxqcQPbM4ph3kVvZ6vDZxk6SfWXnIyR4xakwNn8_wZApsZDE3dYy8yD71tOZyaKx0g7SfLdJ1VbEaDUArFf9wPGaubQj-27QHqGVwXHD5bSfIejeQWayC6QL5Gnxrebj_qG040yEv6sN_VTHYf1p1c-9dP5M/w640-h315/menu.jpg&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Botão APIs e serviços&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;Finalmente, ainda na opção &lt;b&gt;APIs e serviços&lt;/b&gt;, vá em &lt;b&gt;Credenciais&lt;/b&gt;. Se você já tiver uma conta de serviço, clique no e-mail visível no grupo &lt;b&gt;Contas de serviço&lt;/b&gt;, a fim de criar uma chave. Se não, clique em &lt;b&gt;+ Criar credenciais&lt;/b&gt; e &lt;b&gt;Conta de serviço&lt;/b&gt; para criar uma.&lt;/p&gt;&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;border: 1px solid rgb(0, 0, 0);text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMKIdOexFQLh98KGyEtXuWoSGYiWFvra2yuXTGUAdLHUFhUZ_qltLyzqNFusfUJeA7EYV4pOFwJkfzl4uJeigAvy8Mzl_97I83f6tmJhCpxEdWzI6FOf6lGRDh1wGUDRVukoBSvt9aIzgl5wa5IF8k8WdfUwUTFUlCFW4oMRe9kSFV2beXtV61/s1212/contas.jpg&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;634&quot; data-original-width=&quot;1212&quot; height=&quot;335&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMKIdOexFQLh98KGyEtXuWoSGYiWFvra2yuXTGUAdLHUFhUZ_qltLyzqNFusfUJeA7EYV4pOFwJkfzl4uJeigAvy8Mzl_97I83f6tmJhCpxEdWzI6FOf6lGRDh1wGUDRVukoBSvt9aIzgl5wa5IF8k8WdfUwUTFUlCFW4oMRe9kSFV2beXtV61/w640-h335/contas.jpg&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Contas de serviço&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;Depois que visualizar a &lt;b&gt;Conta de serviço&lt;/b&gt;, clique em &lt;b&gt;Chave&lt;/b&gt; e em &lt;b&gt;Adicionar chave&lt;/b&gt;. Selecione &lt;b&gt;Criar nova chave&lt;/b&gt;, deixe marcado &lt;b&gt;JSON&lt;/b&gt; e clique em &lt;b&gt;Criar&lt;/b&gt;. Salve o arquivo JSON numa pasta segura no seu computador, dê um nome a ele, por exemplo &lt;i&gt;credenciais.json&lt;/i&gt;. Em seguida copie o e-mail na chave &lt;b&gt;client_email&lt;/b&gt;&amp;nbsp;do JSON salvo. Ele será usado para compartilhar a planilha no Google Sheets.&lt;/p&gt;&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td style=&quot;border: 1px solid rgb(0, 0, 0);text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqFzGWzOmvM8DeIjnztamdHzHw5JdQvFS3lY1warrD9sCCZ2yOKTfRebVe28Fh_j-3TCULH2XHTzpmwxZTWULtrSuSIz93yc1JhpseV8zzSEQi0GdsGe89777nD9pgTrZb0pxkpcvt9mhRFO5nXTQy-YL-91Plxnf7iUYT0H_9xwrtXlWMbdgJ/s1212/chave.jpg&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;634&quot; data-original-width=&quot;1212&quot; height=&quot;334&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqFzGWzOmvM8DeIjnztamdHzHw5JdQvFS3lY1warrD9sCCZ2yOKTfRebVe28Fh_j-3TCULH2XHTzpmwxZTWULtrSuSIz93yc1JhpseV8zzSEQi0GdsGe89777nD9pgTrZb0pxkpcvt9mhRFO5nXTQy-YL-91Plxnf7iUYT0H_9xwrtXlWMbdgJ/w640-h334/chave.jpg&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Criar uma chave na Conta de serviço&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;h3&gt;3. Conectando a API do Google Sheets&lt;/h3&gt;
  &lt;p&gt;Primeiramente, instale as bibliotecas que serão usadas no código Python:&lt;/p&gt;
  &lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;pip&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;install&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;gspread&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;pandas&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;google&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;auth yfinance&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Em seguida, escreva a função responsável por fazer a conexão com a API do Goolge Sheets. Note que utilizamos o arquivo &lt;i&gt;credenciais.json&lt;/i&gt; que foi obtido na seção anterior:&lt;/p&gt;
    &lt;div class=&quot;code-header&quot;&gt;automatizar_financas.py&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; gspread&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; pandas &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; pd&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; google.oauth2.service_account &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;Credentials&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;conectar_google_sheets&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;():&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;scopes&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;[&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;https://www.googleapis.com/auth/spreadsheets&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;https://www.googleapis.com/auth/drive&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;

    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;creds&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;Credentials&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;from_service_account_file(&lt;/span&gt;
      &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;credenciais.json&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;scopes&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;scopes)&lt;/span&gt;

    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;client&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;gspread&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;authorize(creds)&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;client&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
 

  &lt;h3&gt;4. Script para Atualizar Transações Bancárias&lt;/h3&gt;&lt;div&gt;Aqui é necessário obter um extrato da sua conta por meio do aplicativo do banco. Após a solicitação você irá receber um e-mail com o extrato em três formatos: PDF, CSV e OFX. Nós iremos utilizar o formato CSV. Dois pontos merecem atenção:&lt;/div&gt;&lt;div&gt;a) Não existe uma estrutura padrão. Cada banco usa nomes de campos diferentes e posições diferentes no arquivo;&lt;/div&gt;&lt;div&gt;b) Pode ser necessário fazer uma limpeza (preparação dos dados) antes de fazer a importação. Checamos isso em dois bancos: Nubank e Inter. Nubank envia um extrato onde os campos são separados por vírgula e o Inter usa ponto e vírgula como separador, além de precisar deletar algumas linhas no início do arquivo.&lt;/div&gt;&lt;div&gt;No nosso exemplo usaremos um extrato obtido do banco Inter, usando o seguinte código:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;code-header&quot;&gt;automatizar_financas.py&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; gspread&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; pandas &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; pd&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; google.oauth2.service_account &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;Credentials&lt;/span&gt;

&lt;span style=&quot;color: #6272a4;&quot;&gt;# Código anterior vai aqui&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;importar_extrato_csv&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(arquivo_csv,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;worksheet):&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;df&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;pd&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;read_csv(&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;arquivo_csv,&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;sep&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;;&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;decimal&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;,&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;encoding&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;utf-8&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;on_bad_lines&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;skip&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;for&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;_,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;row&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;df&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;iterrows():&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;worksheet&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;append_row(&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;[&lt;/span&gt;
                &lt;span style=&quot;color: #f8f8f2;&quot;&gt;row[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Data Lançamento&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;],&lt;/span&gt;
                &lt;span style=&quot;color: #f8f8f2;&quot;&gt;row[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Histórico&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot; &quot;&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;row[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Descrição&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;],&lt;/span&gt;
                &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
                &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(row[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Valor&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;replace(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;.&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;replace(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;,&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;.&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)),&lt;/span&gt;
                &lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div class=&quot;post-content&quot;&gt;&lt;br /&gt;&lt;/div&gt;
Esta função deve receber como parâmetros o extrato em CSV e o objeto planilha obtido por meio da conexão com a API Google Sheets.&lt;/div&gt;&lt;div class=&quot;post-content&quot;&gt;A função lê o CSV e o converte em um DataFrame, o qual é precorrido e cada uma de suas linhas é adicionado à planilha Google.&amp;nbsp; Os nomes das colunas no DataFrame são definidos pelo CSV fornecido pelo banco Inter. Para um outro banco esses nomes podem ser diferentes.&lt;/div&gt;&lt;div class=&quot;post-content&quot;&gt;Juntando essas duas funções com o trecho que vai executar:&lt;/div&gt;&lt;div class=&quot;post-content&quot;&gt;&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;code-header&quot;&gt;automatizar_financas.py&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; gspread&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; pandas &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; pd&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; google.oauth2.service_account &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;Credentials&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;conectar_google_sheets&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;():&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;scopes&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;[&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;https://www.googleapis.com/auth/spreadsheets&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;https://www.googleapis.com/auth/drive&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;

    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;creds&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;Credentials&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;from_service_account_file(&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;credenciais.json&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;scopes&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;scopes)&lt;/span&gt;

    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;client&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;gspread&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;authorize(creds)&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;client&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;importar_extrato_csv&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(arquivo_csv,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;worksheet):&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;df&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;pd&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;read_csv(&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;arquivo_csv,&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;sep&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;;&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;decimal&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;,&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;encoding&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;utf-8&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;on_bad_lines&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;skip&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;for&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;_,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;row&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;df&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;iterrows():&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;worksheet&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;append_row(&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;[&lt;/span&gt;
                &lt;span style=&quot;color: #f8f8f2;&quot;&gt;row[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Data Lançamento&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;],&lt;/span&gt;
                &lt;span style=&quot;color: #f8f8f2;&quot;&gt;row[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Histórico&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot; &quot;&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;row[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Descrição&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;],&lt;/span&gt;
                &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt;
                &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(row[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Valor&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;replace(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;.&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;replace(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;,&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;.&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)),&lt;/span&gt;
                &lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;
                    &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Receita&quot;&lt;/span&gt;
                    &lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;float&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;
                        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;row[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Valor&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;replace(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;.&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;replace(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;,&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;.&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
                        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;&amp;gt;=&lt;/span&gt; &lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;
                    &lt;span style=&quot;color: #ff79c6;&quot;&gt;else&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Despesa&quot;&lt;/span&gt;
                &lt;span style=&quot;color: #f8f8f2;&quot;&gt;),&lt;/span&gt;
            &lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;__name__&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
    &lt;span style=&quot;color: #6272a4;&quot;&gt;# Conectar à planilha&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;client&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;conectar_google_sheets()&lt;/span&gt;
    &lt;span style=&quot;color: #6272a4;&quot;&gt;# Abrir a planilha e a aba desejada (case sensitive)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;planilha&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;client&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;open(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Minhas Finanças Pessoais&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;sheet&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;planilha&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;worksheet(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Transações&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #6272a4;&quot;&gt;# Importar o extrato CSV para a aba especificada&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;importar_extrato_csv(&lt;/span&gt;
        &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;./extratos/Extrato-30-09-2025-a-30-10-2025-CSV.csv&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;sheet)&lt;/span&gt;
    &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Importação concluída com sucesso!&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;div class=&quot;post-content&quot;&gt;&lt;br /&gt;&lt;/div&gt;
Atente para os comentários neste trecho. Além da questão de que os nomes das planilhas são &lt;i&gt;case sensitive&lt;/i&gt;, o CSV está numa pasta denominada &lt;i&gt;extratos&lt;/i&gt;.
&lt;div class=&quot;post-content&quot;&gt;&lt;h3&gt;5. Atualizando Preços de Investimentos&lt;/h3&gt;

  &lt;div class=&quot;code-block&quot;&gt;
    &lt;div class=&quot;code-header&quot;&gt;investimentos.py&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; automatizar_financas &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;conectar_google_sheets&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; yfinance &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; yf&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;atualizar_investimentos&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;():&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;client&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;conectar_google_sheets()&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;planilha&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;client&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;open(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Minhas Finanças Pessoais&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;worksheet&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;planilha&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;worksheet(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Investimentos&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;

    &lt;span style=&quot;color: #6272a4;&quot;&gt;# Obter dados atualizados&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;investimentos&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;worksheet&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;get_all_records()&lt;/span&gt;

    &lt;span style=&quot;color: #ff79c6;&quot;&gt;for&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;i,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;investimento&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;enumerate&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(investimentos,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;start&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;):&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;ticker&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;investimento[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Ativo&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;preco_atual&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;obter_preco_atual(ticker)&lt;/span&gt;

        &lt;span style=&quot;color: #6272a4;&quot;&gt;# Atualizar preço atual&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;worksheet&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;update_cell(i,&lt;/span&gt; &lt;span style=&quot;color: #ffb86c;&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;preco_atual)&lt;/span&gt;

        &lt;span style=&quot;color: #6272a4;&quot;&gt;# Calcular e atualizar valor total&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;quantidade&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;investimento[&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Quantidade&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;]&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;valor_total&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;quantidade&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;preco_atual&lt;/span&gt;
        &lt;span style=&quot;color: #f8f8f2;&quot;&gt;worksheet&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;update_cell(i,&lt;/span&gt; &lt;span style=&quot;color: #ffb86c;&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;valor_total)&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;obter_preco_atual&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(ticker):&lt;/span&gt;
    &lt;span style=&quot;color: #6272a4;&quot;&gt;# Exemplo com API do Yahoo Finance&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;ticker_yf&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;yf&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Ticker(ticker&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;.SA&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;info&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;ticker_yf&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;info&lt;/span&gt;
    &lt;span style=&quot;color: #ff79c6;&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;info&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;get(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;regularMarketPrice&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #ffb86c;&quot;&gt;0.0&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;__name__&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;__main__&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;atualizar_investimentos()&lt;/span&gt;
    &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;Atualização de investimentos concluída com sucesso!&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


  &lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Neste módulo utilizamos o pacote &lt;i&gt;yfinance&lt;/i&gt; para obter dados das ações, neste caso, o preço atual. O código lê da planilha &lt;b&gt;Investimentos&lt;/b&gt;, cada ação, busca seu preço atual e atualiza os valores das células de preço e valor total.&lt;/div&gt;&lt;h3&gt;6. Dashboard Automático com Fórmulas&lt;/h3&gt;

  &lt;p&gt;A fim de que os dados sejam resumidos, no Google Sheets, use estas fórmulas na aba &lt;b&gt;Dashboard&lt;/b&gt;:&lt;/p&gt;

  &lt;p&gt;&lt;strong&gt;Saldo Atual:&lt;/strong&gt;&lt;/p&gt;
  &lt;div class=&quot;code-block&quot;&gt;
    &lt;pre&gt;&lt;code&gt;=SOMA(Transações!D:D)&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;p&gt;&lt;strong&gt;Receita do Mês:&lt;/strong&gt;&lt;/p&gt;
  &lt;div class=&quot;code-block&quot;&gt;
    &lt;pre&gt;&lt;code&gt;=SOMA(FILTER(&#39;Transações&#39;!D2:D; &#39;Transações&#39;!E2:E=&quot;Receita&quot;; MÊS(&#39;Transações&#39;!A2:A)=HOJE()))&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;p&gt;&lt;strong&gt;Despesas do Mês:&lt;/strong&gt;&lt;/p&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;pre&gt;&lt;code&gt;=SOMA(FILTER(&#39;Transações&#39;!D2:D; &#39;Transações&#39;!E2:E=&quot;Receita&quot;; MÊS(&#39;Transações&#39;!A2:A)=HOJE()))&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Investimentos:&lt;/strong&gt;&lt;/p&gt;
  &lt;div class=&quot;code-block&quot;&gt;
    &lt;pre&gt;&lt;code&gt;=SOMA(Investimentos!E:E)&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;

  &lt;h3&gt;7. Agendando a Execução Automática&lt;/h3&gt;

    &lt;div class=&quot;code-header&quot;&gt;Visto que a obtenção do extrato bancário não pode ser automatizada, se agendarmos a atualização das transações bancárias, será necessário que você solicite o extrato antes que a tarefa agenda aconteça. Desse modo vamos mostrar apenas o agendamento da atualização dos investimentos. De toda maneira, com poucas alterações podemos incluir o agendamento das transações.&lt;/div&gt;&lt;div class=&quot;code-header&quot;&gt;&amp;nbsp;&lt;/div&gt;&lt;div class=&quot;code-header&quot;&gt;agendamento.py&lt;/div&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; schedule&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; time&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; datetime &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;datetime&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;from&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; investimentos &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #f8f8f2;&quot;&gt;atualizar_investimentos&lt;/span&gt;


&lt;span style=&quot;color: #ff79c6;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;rotina_diaria&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;():&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;atualizar_investimentos()&lt;/span&gt;
    &lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;f&quot;Planilha atualizada em {&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;datetime&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;now()&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;}&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;


&lt;span style=&quot;color: #6272a4;&quot;&gt;# Agendar execução diária às 18:00&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;schedule&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;every()&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;day&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;at(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&quot;09:27&quot;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;do(rotina_diaria)&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;while&lt;/span&gt; &lt;span style=&quot;color: #ff79c6;&quot;&gt;True&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;:&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;schedule&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;run_pending()&lt;/span&gt;
    &lt;span style=&quot;color: #f8f8f2;&quot;&gt;time&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;sleep(&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

  &lt;h3&gt;Dicas para Personalizar&lt;/h3&gt;

  &lt;ol&gt;
    &lt;li&gt;
      &lt;strong&gt;Adapte às suas necessidades:&lt;/strong&gt; Adicione categorias
      específicas do seu estilo de vida
    &lt;/li&gt;
    &lt;li&gt;
      &lt;strong&gt;Múltiplas fontes:&lt;/strong&gt; Conecte com diferentes bancos e
      corretoras
    &lt;/li&gt;
    &lt;li&gt;
      &lt;strong&gt;Backup automático:&lt;/strong&gt; Configure backup semanal da planilha
    &lt;/li&gt;
    &lt;li&gt;
      &lt;strong&gt;Segurança:&lt;/strong&gt; Mantenha suas credenciais em ambiente seguro
    &lt;/li&gt;
  &lt;/ol&gt;

  &lt;div class=&quot;tip-box&quot;&gt;
    &lt;strong&gt;🚀 Próximos Passos:&lt;/strong&gt;
    &lt;ul&gt;
      &lt;li&gt;Adicione gráficos automáticos no Google Sheets&lt;/li&gt;&lt;li&gt;Crie alertas por email&lt;/li&gt;
      &lt;li&gt;Crie relatórios mensais por email&lt;/li&gt;
      &lt;li&gt;Integre com Telegram para notificações&lt;/li&gt;
      &lt;li&gt;Adicione previsões de gastos com &lt;i&gt;machine learning&lt;/i&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/div&gt;

  &lt;h2&gt;Conclusão&lt;/h2&gt;

  &lt;p&gt;
    Com essa automação, você terá suas finanças sempre organizadas e
    atualizadas, podendo focar no que realmente importa: tomar decisões
    inteligentes com seu dinheiro.
  &lt;/p&gt;

  &lt;div class=&quot;tip-box&quot;&gt;
    &lt;strong&gt;💡 Dica final:&lt;/strong&gt; Comece simples e vá incrementando aos
    poucos. O importante é dar o primeiro passo rumo à liberdade financeira!
  &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&quot;post-footer&quot;&gt;
  &lt;p&gt;
    Espero que este guia tenha sido útil! Se tiver dúvidas ou sugestões, deixe
    nos comentários.
  &lt;/p&gt;
  &lt;p&gt;Compartilhe suas dúvidas, implementações e experiências!&lt;/p&gt;
&lt;/div&gt;

&lt;script&gt;
  // Adiciona a data atual automaticamente
  document.getElementById(&#39;current-date&#39;).textContent = new Date().toLocaleDateString(&#39;pt-BR&#39;);
&lt;/script&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://professorcarlos.blogspot.com/feeds/4484556500032792671/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/33774862/4484556500032792671?isPopup=true' title='0 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/4484556500032792671'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/4484556500032792671'/><link rel='alternate' type='text/html' href='http://professorcarlos.blogspot.com/2025/11/como-automatizar-suas-financas-pessoais.html' title='Como Automatizar suas Finanças Pessoais com Google Sheets e Python'/><author><name>Professor Carlos</name><uri>http://www.blogger.com/profile/10347668022241306511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFBZD8558AE67R-CINkytW3INbUINaAhDuZ7GKm_4DSSKeHSpdm3K-TPQrhNPn5B-21wN24ZH8flimqC7ikTzmRrYZBAsyD_JSC8YW8BDJZwmTby9R7LZC9SYkyHkSs7NPVZxXiqEDqjhkRJNxZwqqyZaVgYjISiFpzXsSyGZmL1YFCQ/s220/WhatsApp%20Image%202024-05-17%20at%2017.42.05.jpeg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhBg1_OMAV98BXXAmWoDibCFBI1mF8SRjsdTORbJf_imp2NLu8a9ETrkXA6fY7X_rwp5b8s3jK3NfZi20UBOlNFnd6OrVmHbdSXLhvSkdXL-sk1-5y6YHsiXnBWF89YEQOxyHa_SfuX5DmuKCl_eM5iVtxQSMJ1z1RSmWVGADRkYoW_N5uP5rEn/s72-w640-h429-c/analise-de-estrategia-de-estudo-de-informacao-de-planejamento-de-negocios.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-33774862.post-6777032920443009085</id><published>2011-08-12T14:37:00.008-03:00</published><updated>2025-11-27T16:13:50.505-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="editor de código"/><category scheme="http://www.blogger.com/atom/ns#" term="Lazarus"/><title type='text'>Lazarus - Recursos do Editor de Código</title><content type='html'>&lt;h1&gt;Lazarus - Recursos do Editor de Código&lt;/h1&gt;
Neste post vamos apresentar alguns dos inúmeros recursos que o Editor de Código oferece aos desenvolvedores. O Editor pode ser configurado via menu em &lt;b&gt;Tools | Options | Editor&lt;/b&gt;. Estas opções também podem ser acessadas através do menu de contexto.&lt;div&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3Ev7GEI8C5_49rRu0m5DCUeAKkE0nk6y5bwFyTrfa8fDw83QJqBvSD4DvaYXHtA2TLYBc7BOeOdGLAodD-ijD88Tn0214Tnf360mZ6tnKlGgOjSvZHKDJRabjyw7_ETEZrGEZ/s1600/configeditor.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;480&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3Ev7GEI8C5_49rRu0m5DCUeAKkE0nk6y5bwFyTrfa8fDw83QJqBvSD4DvaYXHtA2TLYBc7BOeOdGLAodD-ijD88Tn0214Tnf360mZ6tnKlGgOjSvZHKDJRabjyw7_ETEZrGEZ/w640-h480/configeditor.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;O detalhamento das opções e configuração do Editor exige um post específico, portanto não iremos nos ater a isso no momento. Queremos falar de recursos que a ferramenta oferece para facilitar o trabalho do programador.&lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;&lt;i&gt;Syncron&lt;/i&gt;&lt;/b&gt;&lt;/h2&gt;
Esta é uma opção onde podemos rapidamente modificar o nome de um identificador em um trecho de código &amp;nbsp;selecionado.&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3axCPEh7vBgNKW-jLuzgCzRFo-FbY97ShCjDsL7yHFHuJg_OH3ThOq5S50bBtCjlJvl6_Tug6fBaork1ln7tmfhFXyuwLkyDmKgnb18RCVUc4E7bEi8ARBqOiZTR9dJhPA2NP/s1600/syncron1.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;162&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3axCPEh7vBgNKW-jLuzgCzRFo-FbY97ShCjDsL7yHFHuJg_OH3ThOq5S50bBtCjlJvl6_Tug6fBaork1ln7tmfhFXyuwLkyDmKgnb18RCVUc4E7bEi8ARBqOiZTR9dJhPA2NP/w640-h162/syncron1.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Quando você seleciona um trecho do código (ou o código inteiro), no lado esquerdo do Editor aparece um ícone com uma caneta. Clique nesse ícone ou tecle &lt;i&gt;CTRL + J&lt;/i&gt;. A área selecionada muda de cor e o cursor é movido para uma instância do identificador, destacando cada uma de suas ocorrências naquela área. Modifique o identificador e cada ocorrência dele será alterada automaticamente.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqmJ6pxkDGmluuxgp1_NN7u8ZwtP-R16xfJfb1lT65bUJlCiZkC6J1Zy0S6SR54lfXZNg7zG5i6twIn1Tb7tErjNH5zRV1ewGask03FSGDc652YsTpCwqXZGIoLblfe_9wpiTU/s1600/syncron2.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;128&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqmJ6pxkDGmluuxgp1_NN7u8ZwtP-R16xfJfb1lT65bUJlCiZkC6J1Zy0S6SR54lfXZNg7zG5i6twIn1Tb7tErjNH5zRV1ewGask03FSGDc652YsTpCwqXZGIoLblfe_9wpiTU/w640-h128/syncron2.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Volte a clicar no ícone ou tecle &lt;i&gt;ESC&lt;/i&gt; para sair do modo &lt;i&gt;syncron&lt;/i&gt;.&lt;br /&gt;
&lt;br /&gt;
O Code Completion é um dos recursos que pode poupar muito tempo de digitação. Dependendo da posição do cursor no Editor, várias funções podem ser chamadas.&lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;Complementação de classe&lt;/b&gt;&lt;/h2&gt;
Se você escreveu uma classe incluindo propriedades e métodos, este recurso irá adicionar o corpo do método, variáveis e métodos de acesso às propriedades, etc. Suponha que você tenha criado a seguinte classe:&lt;br /&gt;
&lt;br /&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;TPessoa &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #8be9fd;&quot;&gt;TObject&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;constructor&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;Create&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;destructor&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;Destroy&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;override;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;property&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; nome&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;string;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;br /&gt;
Posicione o cursor no interior da classe e pressione &lt;i&gt;CTRL + SHIFT + C&lt;/i&gt;. O código seguinte será gerado, e o cursor será movido para que o corpo do primeiro método seja digitado.&lt;br /&gt;
&lt;br /&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;type&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;{ TPessoa }&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  TPessoa &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #8be9fd;&quot;&gt;TObject&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;private&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    Fnome&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;string;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;procedure&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;Setnome&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(AValue&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;published&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;constructor&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;Create&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;destructor&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;Destroy&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;override;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;property&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; nome&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;read&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;Fnome&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;write&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;Setnome&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;implementation&lt;/span&gt;

&lt;span style=&quot;color: #6272a4;&quot;&gt;{ TPessoa }&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;procedure&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;TPessoa&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;Setnome&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(AValue&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; Fnome&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;AValue &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;then&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;exit;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  Fnome&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;AValue&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;constructor&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;TPessoa&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;Create&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;destructor&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;TPessoa&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;Destroy&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;inherited&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; Destroy&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;Complementação de declaração de variável&lt;/b&gt;&lt;/h2&gt;
Se o cursor é posicionado em um identificador e for pressionado &lt;i&gt;CTRL + SHIFT + C&lt;/i&gt;, é adicionado ao código uma declaração de variável local. Por exemplo, imagine o seguinte código fonte:&lt;br /&gt;
&lt;br /&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;procedure&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;TForm1&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;FormClick&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(Sender&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd;&quot;&gt;TObject&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  i &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;br /&gt;
Posicione o cursor na variável &lt;i&gt;i&lt;/i&gt; (antes ou depois) e pressione &lt;i&gt;CTRL + SHIFT + C&lt;/i&gt;. O código será modificado e ficará assim:&lt;br /&gt;
&lt;br /&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;procedure&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;TForm1&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;FormClick&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(Sender&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd;&quot;&gt;TObject&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;var&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  i&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd;&quot;&gt;Integer&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  i &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;br /&gt;
O tipo da variável é deduzida a partir da expressão.&lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;Message Composer&lt;/b&gt;&lt;/h2&gt;
O &lt;b&gt;Message Composer&lt;/b&gt; é uma ferramenta que auxilia na criação de caixas de diálogos, &amp;nbsp;tais &lt;b&gt;ShowMessage&lt;/b&gt;, &lt;b&gt;MessageDlg&lt;/b&gt; e &lt;b&gt;InputBox&lt;/b&gt;. Para isso é necessário instalar o pacote &lt;i&gt;messagecomposerpkg&lt;/i&gt;. Vá em &lt;b&gt;Package | Install/Uninstall packages&lt;/b&gt;. Na lista &lt;b&gt;Do not install&lt;/b&gt; localize e selecione&amp;nbsp;&lt;i&gt;messagecomposerpkg&lt;/i&gt;. Clique em &lt;b&gt;Install selection&lt;/b&gt; e depois em &lt;b&gt;Save and rebuild IDE&lt;/b&gt;. Em seguida clique no botão &lt;b&gt;Continue&lt;/b&gt; para confirmar. Após o IDE ser reiniciado uma nova opção será adicionada ao menu &lt;b&gt;Source&lt;/b&gt;. Você também pode usar o atalho &lt;i&gt;CTRL + M&lt;/i&gt;. Esta opção irá abrir a seguinte janela:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwwPbLVmKJtb4GtYiKx5ut_mX7UBInO6c7JmeLQRW9ZFkexIqfeA74TReeUxAXB5rd5iYxmPR6N0EHZqJZyv2msmKRoG4mGyQ4aFXIL0WpzpRcU7BcrKlwshGZ_j64xJYQEE4d/s1600/composer.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;450&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwwPbLVmKJtb4GtYiKx5ut_mX7UBInO6c7JmeLQRW9ZFkexIqfeA74TReeUxAXB5rd5iYxmPR6N0EHZqJZyv2msmKRoG4mGyQ4aFXIL0WpzpRcU7BcrKlwshGZ_j64xJYQEE4d/w640-h450/composer.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Após escolher o tipo de mensagem, os parâmetros demais opções, confirme em OK e o diálogo selecionado será adicionado no seu código na posição do cursor.&lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;Dicas para funções e procedimentos&lt;/b&gt;&lt;/h2&gt;
Se você não lembra dos parâmetros, ordem, tipos de dados de uma função ou procedimento, digite a mesma e dentro dos parênteses tecle &lt;i&gt;CTRL + SHIFT + ESPAÇO&lt;/i&gt;. Irá aparecer uma janela de dica com os argumentos:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdVVNK7jMitHAWuOdW1BBmtajaSx6Bkkx8LxBZrSx2ZDuu2MlxKSsOtYHuLoH7LMc5ue4fhmWNMxmjJadvxTwf70me8cxdZI4UKCSmNV-xTwHSaCCggX0Ta-j43lZc2ckYf8Pi/s1600/dicas.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;130&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdVVNK7jMitHAWuOdW1BBmtajaSx6Bkkx8LxBZrSx2ZDuu2MlxKSsOtYHuLoH7LMc5ue4fhmWNMxmjJadvxTwf70me8cxdZI4UKCSmNV-xTwHSaCCggX0Ta-j43lZc2ckYf8Pi/w640-h130/dicas.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;À medida que um argumentos é digitado, o próximo fica em destaque na janela. Um botão à direita permite colar os parâmetros no código fonte.&lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;Templates&lt;/b&gt;&lt;/h2&gt;
Templates são modelos que podem ser inseridos no código. Por exemplo, suponha que você deseja inserir um comando &lt;b&gt;case&lt;/b&gt; no seu código. Digite &lt;i&gt;casee&lt;/i&gt; no ponto onde deseja inserir e tecle &lt;i&gt;CRTL + J&lt;/i&gt;. Será inserido o seguinte código, e o cursor será posicionado no local onde deve ser inserida a variável do comando.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjO37EvJp2id-hhqEfrmPtyzdubTWaDvJNHz3vwhI3x36whMthHL36SgnNGmAY7OWTDA-f_hRbo8_pTmaW2hc4TN7i7d6rTnG7fg5TSFhho0YvwVkxE7WX9IwlQWK-ZJ5kTo7wK/s1600/template.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;196&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjO37EvJp2id-hhqEfrmPtyzdubTWaDvJNHz3vwhI3x36whMthHL36SgnNGmAY7OWTDA-f_hRbo8_pTmaW2hc4TN7i7d6rTnG7fg5TSFhho0YvwVkxE7WX9IwlQWK-ZJ5kTo7wK/w640-h196/template.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Existem vários modelos prontos, mas você pode criar os seus próprios templates usando a opção &lt;i&gt;Tools | Code Templates&lt;/i&gt;. Na janela que será aberta você usará o botão &lt;b&gt;Add&lt;/b&gt; para adicionar novo modelo.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8jozer4HkGimJYzdVX8jl_0Hz5m-yAlbTmY4UYyc72bv-ooCv_jJRv4lIjwN7Os__eZ5zeQsWJGdd-v0wbbk5Nl6J_n4c1Xb6XaQ4xEEQeYBTHz2f-HslXd-zQmZ_Vo6QXkej/s1600/codetemplate.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;640&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8jozer4HkGimJYzdVX8jl_0Hz5m-yAlbTmY4UYyc72bv-ooCv_jJRv4lIjwN7Os__eZ5zeQsWJGdd-v0wbbk5Nl6J_n4c1Xb6XaQ4xEEQeYBTHz2f-HslXd-zQmZ_Vo6QXkej/w628-h640/codetemplate.png&quot; width=&quot;628&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Clique &lt;a href=&quot;https://wiki.lazarus.freepascal.org/IDE_Window:_Code_Templates&quot;&gt;aqui&lt;/a&gt; para ver um rápido tutorial de como criar novos modelos. Por enquanto é isso que temos. Aproveite os recursos e explore mais possibilidades com o Lazarus&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://professorcarlos.blogspot.com/feeds/6777032920443009085/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/33774862/6777032920443009085?isPopup=true' title='9 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/6777032920443009085'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/6777032920443009085'/><link rel='alternate' type='text/html' href='http://professorcarlos.blogspot.com/2011/08/lazarus-recursos-do-editor-de-codigo.html' title='Lazarus - Recursos do Editor de Código'/><author><name>Professor Carlos</name><uri>http://www.blogger.com/profile/10347668022241306511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFBZD8558AE67R-CINkytW3INbUINaAhDuZ7GKm_4DSSKeHSpdm3K-TPQrhNPn5B-21wN24ZH8flimqC7ikTzmRrYZBAsyD_JSC8YW8BDJZwmTby9R7LZC9SYkyHkSs7NPVZxXiqEDqjhkRJNxZwqqyZaVgYjISiFpzXsSyGZmL1YFCQ/s220/WhatsApp%20Image%202024-05-17%20at%2017.42.05.jpeg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3Ev7GEI8C5_49rRu0m5DCUeAKkE0nk6y5bwFyTrfa8fDw83QJqBvSD4DvaYXHtA2TLYBc7BOeOdGLAodD-ijD88Tn0214Tnf360mZ6tnKlGgOjSvZHKDJRabjyw7_ETEZrGEZ/s72-w640-h480-c/configeditor.png" height="72" width="72"/><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-33774862.post-1785491180373991321</id><published>2011-07-23T08:40:00.007-03:00</published><updated>2025-11-27T16:14:41.266-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="dicionário de dados"/><category scheme="http://www.blogger.com/atom/ns#" term="Lazarus"/><title type='text'>Lazarus - Usando Dicionários de Dados</title><content type='html'>&lt;h1 style=&quot;text-align: left;&quot;&gt;Lazarus - Usando Dicionários de Dados&lt;/h1&gt;Nos dois posts anteriores:&lt;br /&gt;
&lt;a href=&quot;https://professorcarlos.blogspot.com/2011/07/lazarus-o-lazarus-data-desktop.html&quot;&gt;Lazarus - O Lazarus Data Desktop&lt;/a&gt; e&lt;br /&gt;
&lt;a href=&quot;https://professorcarlos.blogspot.com/2011/07/lazarus-dicionarios-de-dados.html&quot;&gt;Lazarus - Dicionário de Dados&lt;/a&gt;&lt;br /&gt;
estudamos a ferramenta Lazarus Data Desktop e como criar dicionários de dados. Neste artigo iremos mostrar como usar os dicionários nas aplicações desenvolvidas usando Lazarus.&lt;br /&gt;
Como já falamos, um dicionário de dados descreve entre outras coisas os atributos dos campos das tabelas de um esquema de banco de dados. E essa descrição, também chamada de metadados, pode ser usada nas aplicações para evitar que tenhamos que repetir essas definições em cada &lt;b&gt;DataSet&lt;/b&gt;. Assim, vamos supor que já exista um dicionário de dados criado para um tabela chamada PRODUTO. E vamos mostrar como usar o dicionário de dados em tempo de execução e como aplicá-lo em tempo de desenvolvimento.&lt;br /&gt;
O primeiro passo é o mesmo necessário para acessar qualquer tabela sem usar o recurso do dicionário de dados. Supondo que estejamos usando &lt;b&gt;ZeosLib&lt;/b&gt;, coloque todos os componentes que são precisos, no &lt;b&gt;Data Module&lt;/b&gt;: &lt;b&gt;ZConnection&lt;/b&gt;, &lt;b&gt;ZQuery&lt;/b&gt; e um &lt;b&gt;DataSource&lt;/b&gt;. Faça todas as configurações exigidas. Agora crie os campos persistentes &lt;b&gt;TFields&lt;/b&gt; para a &lt;b&gt;ZQuery &lt;/b&gt;- este é um procedimento obrigatório para o funcionamento do dicionário de dados. Dê um duplo clique na &lt;b&gt;ZQuery&lt;/b&gt; e você verá uma pequena janela como na figura abaixo:&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-zQNijSsFG4qI93cpjSHBcLrtIJ3EgzXADU6DBsDm8Ig3MsF1g2nb_qnuO-fr_7FkzNBHTDViIXvhUlSCvNT8X_JHjgRcCZ-Fa0nEJhufmciePCIOWkK_bVSzhZYsniUeeN38/s1600/tfields.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-zQNijSsFG4qI93cpjSHBcLrtIJ3EgzXADU6DBsDm8Ig3MsF1g2nb_qnuO-fr_7FkzNBHTDViIXvhUlSCvNT8X_JHjgRcCZ-Fa0nEJhufmciePCIOWkK_bVSzhZYsniUeeN38/s320/tfields.png&quot; width=&quot;234&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Clique no botão com o sinal de &lt;b&gt;+&lt;/b&gt; ou pressione &lt;i&gt;CTRL + INS&lt;/i&gt; para inserir os campos. Uma nova janela mostrando todos os campos da tabela correspondente será mostrada, como a figura a seguir:&lt;div&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeyer4yrUP85LkoGuoffJxPI0eM4rE6mmBh-FocIe5-iVP6iyNxVF7nJNS17aJeo6ID8WmIWIQ2AfQ9DGvY2PNXKTQnIWVTSJhU5srzVWP94w71tcHJf4pLHAwqdgkcp8jsMSP/s1600/tfields1.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeyer4yrUP85LkoGuoffJxPI0eM4rE6mmBh-FocIe5-iVP6iyNxVF7nJNS17aJeo6ID8WmIWIQ2AfQ9DGvY2PNXKTQnIWVTSJhU5srzVWP94w71tcHJf4pLHAwqdgkcp8jsMSP/s1600/tfields1.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Selecione todos os campos e pressione o botão &lt;b&gt;Create&lt;/b&gt;.&lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;Usando dicionário de dados em run-time&lt;/b&gt;&lt;/h2&gt;
Uma maneira de usar o dicionário de dados em tempo de execução é inserir o código, por exemplo, no evento &lt;b&gt;OnCreate&lt;/b&gt; do &lt;b&gt;Data Module&lt;/b&gt;. A classe &lt;b&gt;TFPDataDictionary&lt;/b&gt; está localizada na &lt;i&gt;unit&lt;/i&gt; &lt;b&gt;fpdatadict&lt;/b&gt;. Portanto, é necessário declarar esta &lt;i&gt;unit&lt;/i&gt; na cláusula uses no módulo onde será instanciada &lt;b&gt;TFPDataDictionary&lt;/b&gt;. Assim, nosso código poderá ficar assim:&lt;br /&gt;
&lt;br /&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;procedure&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;TdmData&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;DataModuleCreate&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(Sender&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd;&quot;&gt;TObject&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;var&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  Dict &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TFPDataDictionary&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  Dict &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TFPDataDictionary&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Create&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;// carrega o arquivo do dicionário de dados&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  Dict&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;LoadFromFile(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;produto.fpd&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;// aplica o dicionário ao DataSet&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  Dict&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;ApplyToDataset(queProduto)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;// libera a memória usada pelo dicionário de dados&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  Dict&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Free&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;br /&gt;
Num form você pode inserir um &lt;b&gt;TDBGrid&lt;/b&gt;, onde serão mais facilmente visualizados os efeitos que desejamos. &amp;nbsp;Associe essa grade ao &lt;b&gt;DataSource&lt;/b&gt; que está ligado ao &lt;b&gt;DataSet&lt;/b&gt; onde foi aplicado o dicionário.&lt;br /&gt;
Agora execute o programa e você poderá constatar que todas as definições feitas no dicionário de dados serão assumidas pelos &lt;b&gt;TFields&lt;/b&gt; que foram criados.&lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;Usando dicionário de dados em tempo de projeto&lt;/b&gt;&lt;/h2&gt;
Neste caso, o primeiro passo é registrar no projeto o dicionário desejado. Isso é feito usando a opção de menu &lt;b&gt;Project &amp;gt; Data Dictionary &amp;gt; Set ...&lt;/b&gt;&amp;nbsp;Quando selecionar este item de menu um diálogo será aberto:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhiE1O8cEf-lglB1EyC5Fv51ETVtDACxbPrWMdh-uo2k92Fos-0QvZXEDZ4Cz0-WJ3wcIOe0KY8r9gskq2TXFpyQiC9j0HkLVp395dHw7W_bsowzxRXKx4uw6NzGtibaL-ydDE2/s1600/setdict.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;288&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhiE1O8cEf-lglB1EyC5Fv51ETVtDACxbPrWMdh-uo2k92Fos-0QvZXEDZ4Cz0-WJ3wcIOe0KY8r9gskq2TXFpyQiC9j0HkLVp395dHw7W_bsowzxRXKx4uw6NzGtibaL-ydDE2/w640-h288/setdict.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Marque a caixa &lt;b&gt;Use Data dictionary for this project&lt;/b&gt;. E selecione o dicionário de dados desejado. Confirme pressionando &lt;b&gt;OK&lt;/b&gt;.&lt;br /&gt;
Agora localize o &lt;b&gt;DataSet&lt;/b&gt; e clique com o botão direito sobre ele. Selecione o item &lt;b&gt;Data dictionary&lt;/b&gt; e depois em &lt;b&gt;Apply&lt;/b&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8S8uFO9yMP0zhxEDRzKnNFnAYsdwVY-tktZ1iIu7LaxyN1V8N_QUZSa96e9DlclaIhhghoMgKA6lAQfYBIyNqz2iVdbvjm_WW_U8VG9ao5Q-1g8X5YMBL4zkSO4K5JHopQvzn/s1600/menulocal.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;640&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8S8uFO9yMP0zhxEDRzKnNFnAYsdwVY-tktZ1iIu7LaxyN1V8N_QUZSa96e9DlclaIhhghoMgKA6lAQfYBIyNqz2iVdbvjm_WW_U8VG9ao5Q-1g8X5YMBL4zkSO4K5JHopQvzn/w474-h640/menulocal.png&quot; width=&quot;474&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Isso irá aplicar o dicionário ao &lt;b&gt;DataSet&lt;/b&gt;, da mesma forma que fizemos usando código. Agora podemos executar o programa e aproveitar o recurso.&lt;br /&gt;
Post escrito tendo como referência o livro&amp;nbsp;&lt;b&gt;Lazarus - the Complete Guide&lt;/b&gt;, publicado pela&amp;nbsp;&lt;a href=&quot;http://www.blaisepascal.eu/index.php?actie=./subscribers/lazarusbookinfoEnglish&quot;&gt;Blaise Pascal Magazine&lt;/a&gt;.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://professorcarlos.blogspot.com/feeds/1785491180373991321/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/33774862/1785491180373991321?isPopup=true' title='0 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/1785491180373991321'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/1785491180373991321'/><link rel='alternate' type='text/html' href='http://professorcarlos.blogspot.com/2011/07/lazarus-usando-dicionarios-de-dados.html' title='Lazarus - Usando Dicionários de Dados'/><author><name>Professor Carlos</name><uri>http://www.blogger.com/profile/10347668022241306511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFBZD8558AE67R-CINkytW3INbUINaAhDuZ7GKm_4DSSKeHSpdm3K-TPQrhNPn5B-21wN24ZH8flimqC7ikTzmRrYZBAsyD_JSC8YW8BDJZwmTby9R7LZC9SYkyHkSs7NPVZxXiqEDqjhkRJNxZwqqyZaVgYjISiFpzXsSyGZmL1YFCQ/s220/WhatsApp%20Image%202024-05-17%20at%2017.42.05.jpeg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-zQNijSsFG4qI93cpjSHBcLrtIJ3EgzXADU6DBsDm8Ig3MsF1g2nb_qnuO-fr_7FkzNBHTDViIXvhUlSCvNT8X_JHjgRcCZ-Fa0nEJhufmciePCIOWkK_bVSzhZYsniUeeN38/s72-c/tfields.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-33774862.post-2398989704483513813</id><published>2011-07-10T23:22:00.006-03:00</published><updated>2025-11-27T08:19:14.890-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Lazarus"/><title type='text'>Lazarus - Dicionários de Dados</title><content type='html'>&lt;h1 style=&quot;text-align: left;&quot;&gt;Lazarus - Dicionários de Dados&lt;/h1&gt;Dicionários de dados são repositórios com metadados sobre bancos de dados, que descrevem a estrutura básica de um esquema de banco de dados. Num dicionário de dados as seguintes informações são armazenadas: as tabelas e os campos correspondentes, e os índices existentes nas tabelas. Além disso, os dicionários de dados contém atributos que definem como o conteúdo dos campos devem ser visualizados: número de casas decimais, o rótulo, a dica (&lt;i&gt;hint&lt;/i&gt;) mostrada, tamanho do campo, entre outros.&lt;br /&gt;
Um dicionário de dados pode ser usado por uma aplicação Lazarus para definir as propriedades dos objetos &lt;b&gt;TField&lt;/b&gt;; pode ser usado para criar comandos SQL, por exemplo, para criar o banco de dados descrito; e, a partir da comparação entre dois dicionários, as diferenças entre eles podem ser usadas para criar um script de atualização.&lt;br /&gt;
Os dicionários de dados podem ser mantidos usando o Lazarus Data Desktop, que começamos a descrever no post &lt;a href=&quot;https://professorcarlos.blogspot.com/2011/07/lazarus-o-lazarus-data-desktop.html&quot;&gt;Lazarus - o Lazarus Data Desktop&lt;/a&gt;. Usando esta ferramenta podemos criar um dicionário novinho em folha ou importá-lo a partir de um banco de dados existente.&lt;br /&gt;
&lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;Criando um Dicionário de Dados&lt;/b&gt;&lt;/h2&gt;
&lt;br /&gt;
Para criar um novo dicionário clique no botão correspondente na barra de ferramentas conforme mostra a figura, ou tecle &lt;i&gt;CTRL + N&lt;/i&gt;.&lt;div&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiuvkxOm2P1tBxaSt1Za21AZuFR0_YTaBCgtvsfpl8mTkv8hLstSEBeT8FVm-4XTw5r8hJHxH9KyafpkW2JLqdS3DLC1NmRSZJJZnaxQ2jE9RosM7Z6c5EAIJlSHFLtRcDS9X_8/s1600/novo_dict.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;430&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiuvkxOm2P1tBxaSt1Za21AZuFR0_YTaBCgtvsfpl8mTkv8hLstSEBeT8FVm-4XTw5r8hJHxH9KyafpkW2JLqdS3DLC1NmRSZJJZnaxQ2jE9RosM7Z6c5EAIJlSHFLtRcDS9X_8/w640-h430/novo_dict.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;Isso irá criar uma nova aba. Clique com o botão direito no interior da aba e em seguida na opção &lt;i&gt;New Table&lt;/i&gt;, ou tecle &lt;i&gt;CTRL + T&lt;/i&gt;, para criar uma nova tabela. Digite o nome da tabela na caixa de diálogo e confirme.&lt;br /&gt;
Agora clique com o botão direito sobre o nome da tabela recém-criada e escolha a opção &lt;i&gt;New Field&lt;/i&gt;. Digite o nome do campo e confirme. Um novo campo será criado e um formulário para definir as propriedades do campo será aberto no lado direito da interface, como podemos ver na figura abaixo.&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhGD2OXb6AZI4bxRywzcBo1m8jzvN90HmhJTxv5DhPdXuxFxKo-kO_bQvhpx_4FisgNaEaugBd5P9qiGZJlmC8lsNcLOCIxpoPWSD5ix-cp2XTpeCxkhygYOeGYY_xVP6N1IyXM/s1600/novo_campo.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;454&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhGD2OXb6AZI4bxRywzcBo1m8jzvN90HmhJTxv5DhPdXuxFxKo-kO_bQvhpx_4FisgNaEaugBd5P9qiGZJlmC8lsNcLOCIxpoPWSD5ix-cp2XTpeCxkhygYOeGYY_xVP6N1IyXM/w640-h454/novo_campo.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;Algumas propriedades que podem ser definidas, entre outras:&lt;br /&gt;
&lt;b&gt;DisplayName&lt;/b&gt; - rótulo do campo que poderá ser visualizado em uma &lt;b&gt;TDBGrid&lt;/b&gt;, por exemplo;&lt;br /&gt;
&lt;b&gt;DisplayWidth&lt;/b&gt; - tamanho com que o campo será exibido;&lt;br /&gt;
&lt;b&gt;FieldType&lt;/b&gt; - tipo de dado do campo;&lt;br /&gt;
&lt;br /&gt;
Após criar todas as tabelas e campos, tecle &lt;i&gt;CTRL + S&lt;/i&gt; para salvar.&lt;br /&gt;
&lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;Gerando comandos SQL a partir do Dicionário&lt;/b&gt;&lt;/h2&gt;
&lt;br /&gt;
Para gerar os comandos SQL pressione o botão correspondente conforme mostra a figura abaixo ou tecle &lt;i&gt;CTRL + G&lt;/i&gt;.&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMPuWXT5CbCNHfSq2GJNQngIeXyZP220GNCNY0tv8rJKIF0xZwde2KHRpx7X6nEZjaKFA6IHylOgPuHLP6EGf-QlWQdFkzIP-8V-dhaUIXd2l374NULhTJeXyb-Tr5ZFOQoSRn/s1600/gerar_sql.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;454&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMPuWXT5CbCNHfSq2GJNQngIeXyZP220GNCNY0tv8rJKIF0xZwde2KHRpx7X6nEZjaKFA6IHylOgPuHLP6EGf-QlWQdFkzIP-8V-dhaUIXd2l374NULhTJeXyb-Tr5ZFOQoSRn/w640-h454/gerar_sql.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;Essa ação irá abrir um novo formulário. Escolha a tabela para a qual deseja gerar os comandos SQL. Na lista &lt;i&gt;Key fields&lt;/i&gt; selecione o(s) campo(s) que será chave primária. Na lista &lt;i&gt;Select/Update/Insert fields&lt;/i&gt; selecione o(s) campo(s) que serão usados nos comandos SELECT, UPDATE e INSERT. Pressione o botão &lt;i&gt;Generate SQL&lt;/i&gt;. Clique em cada uma das abas para ver os comandos gerados. Na figura seguinte mostramos o comando CREATE TABLE que foi gerado.&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdVCE2tQPYODptqolREgOpVQnxjCz1CTgK9Sm7LWwRWP_d4ikOnIFpqKxosSQDq-EbomzZbNu-wEG5GhSkaVQILPcMYaUdjmbiMnTTDuUS7cRGTODRTEccU7y7VUXLwqP4X7ll/s1600/comandosql.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;334&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdVCE2tQPYODptqolREgOpVQnxjCz1CTgK9Sm7LWwRWP_d4ikOnIFpqKxosSQDq-EbomzZbNu-wEG5GhSkaVQILPcMYaUdjmbiMnTTDuUS7cRGTODRTEccU7y7VUXLwqP4X7ll/w640-h334/comandosql.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;Importando o Dicionário de Dados&lt;/b&gt;&lt;/h2&gt;
&lt;br /&gt;
Se já tivermos um banco de dados criado, podemos criar o dicionário a partir dele - processo que é chamado engenharia reversa.&lt;br /&gt;
Inicialmente crie uma conexão para o banco de dados existente conforme mostramos em&amp;nbsp;&lt;a href=&quot;https://professorcarlos.blogspot.com/2011/07/lazarus-o-lazarus-data-desktop.html&quot;&gt;Lazarus - o Lazarus Data Desktop&lt;/a&gt;. Em seguida selecione no menu &lt;i&gt;Dictionary &amp;gt; Import &amp;gt; From connection&lt;/i&gt;. As conexões criadas na ferramenta estarão disponíveis nessa opção. Escolha aquela para a qual você precisa criar o dicionário.&lt;br /&gt;
Uma caixa de diálogo será aberta mostrando as tabelas do esquema. Selecione as tabelas desejadas, deixe marcada a caixa &lt;i&gt;Update existing tables&lt;/i&gt; para atualizar tabelas que já existem e confirme.&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9HGsRtXR7eXgoPlQbwzZTNYsK47iQuaLm6uCnYn_HNdxXtyJG5exnbNCcXpS2eRxc176F-5Zy4OrmJuoiywcJQHdsAitlZi0pvRGOrBxVT0F8cz-DqqCNe1H4yPT5rupPV4a2/s1600/importar.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;504&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9HGsRtXR7eXgoPlQbwzZTNYsK47iQuaLm6uCnYn_HNdxXtyJG5exnbNCcXpS2eRxc176F-5Zy4OrmJuoiywcJQHdsAitlZi0pvRGOrBxVT0F8cz-DqqCNe1H4yPT5rupPV4a2/w640-h504/importar.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;Se houver um dicionário ativo na ferramenta, você será questionado se deseja atualizar ou criar um novo. Clique &lt;i&gt;CTRL + S&lt;/i&gt; para salvar o dicionário. Os dicionários de dados são salvos com a extensão &lt;b&gt;.fpd&lt;/b&gt;. Você pode visualizar o arquivo de dicionário de dados usando qualquer programa que abre arquivos texto.&lt;br /&gt;
Fica para o próximo post a utilização de dicionários de dados em aplicações Lazarus.&lt;br /&gt;
Post escrito tendo como referência o livro&amp;nbsp;&lt;b&gt;Lazarus - the Complete Guide&lt;/b&gt;, publicado pela&amp;nbsp;&lt;a href=&quot;https://www.blaisepascal.eu/index.php?actie=./subscribers/lazarusbookinfoEnglish&quot;&gt;Blaise Pascal Magazine&lt;/a&gt;.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://professorcarlos.blogspot.com/feeds/2398989704483513813/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/33774862/2398989704483513813?isPopup=true' title='1 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/2398989704483513813'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/2398989704483513813'/><link rel='alternate' type='text/html' href='http://professorcarlos.blogspot.com/2011/07/lazarus-dicionarios-de-dados.html' title='Lazarus - Dicionários de Dados'/><author><name>Professor Carlos</name><uri>http://www.blogger.com/profile/10347668022241306511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFBZD8558AE67R-CINkytW3INbUINaAhDuZ7GKm_4DSSKeHSpdm3K-TPQrhNPn5B-21wN24ZH8flimqC7ikTzmRrYZBAsyD_JSC8YW8BDJZwmTby9R7LZC9SYkyHkSs7NPVZxXiqEDqjhkRJNxZwqqyZaVgYjISiFpzXsSyGZmL1YFCQ/s220/WhatsApp%20Image%202024-05-17%20at%2017.42.05.jpeg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiuvkxOm2P1tBxaSt1Za21AZuFR0_YTaBCgtvsfpl8mTkv8hLstSEBeT8FVm-4XTw5r8hJHxH9KyafpkW2JLqdS3DLC1NmRSZJJZnaxQ2jE9RosM7Z6c5EAIJlSHFLtRcDS9X_8/s72-w640-h430-c/novo_dict.png" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-33774862.post-1919385246213018284</id><published>2011-07-04T14:58:00.007-03:00</published><updated>2025-11-27T16:15:31.593-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="data desktop"/><category scheme="http://www.blogger.com/atom/ns#" term="Lazarus"/><title type='text'>Lazarus - O Lazarus Data Desktop</title><content type='html'>&lt;h1 style=&quot;text-align: left;&quot;&gt;Lazarus - O Lazarus Data Desktop&lt;/h1&gt;Se em suas aplicações você utiliza diversos bancos de dados, é provável que também precise de uma ferramenta específica para cada SGBD, com a finalidade de criar tabelas, criar comandos DML e DDL, entre outros. A fim de centralizar todas as tarefas do desenvolvedor no Ambiente de Desenvolvimento, o Lazarus oferece o Lazarus Data Desktop. Também chamado Database Desktop, esta ferramenta dispõe de vários recursos, dentre os quais destacamos:&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;Acesso a todos os bancos de dados suportados pelo Lazarus;&lt;/li&gt;
&lt;li&gt;Criação de um dicionário de dados onde as propriedades de &lt;b&gt;TFields&lt;/b&gt; podem ser armazenadas;&lt;/li&gt;
&lt;li&gt;Criação de comandos DDL e DML a partir do dicionário de dados;&lt;/li&gt;
&lt;li&gt;Consulta à tabelas;&lt;/li&gt;
&lt;li&gt;Exportação de dados de tabelas para diversos formatos: XML, JSON, CSV, entre outros.&lt;/li&gt;
&lt;/ul&gt;Para usar esta ferramenta. primeiramente é necessário compilá-la. Ela está localizada em&amp;nbsp;&lt;i&gt;tools/lazdatadesktop&lt;/i&gt;, no diretório home do Lazarus. Abra então o projeto &lt;i&gt;lazdatadesktop&lt;/i&gt; e compile-o.&lt;br /&gt;
Para integrar a ferramenta ao IDE, abra e instale o pacote &lt;i&gt;lazDataDict&lt;/i&gt;, localizado no diretório &lt;i&gt;components/datadict&lt;/i&gt;. Após isso o IDE será estendido em três novas opções:&lt;br /&gt;
&lt;ol&gt;&lt;li&gt;Um novo item de menu será registrado no menu &lt;i&gt;Project &lt;/i&gt;- com o nome&amp;nbsp;&lt;i&gt;Data Dictionary&lt;/i&gt;. Esta opção irá permitir que escolhamos um dicionário de dados para o projeto;&lt;/li&gt;
&lt;li&gt;No menu &lt;i&gt;Tools&lt;/i&gt; será acrescentado o item de menu &lt;i&gt;Database Desktop&lt;/i&gt;, que executa o Lazarus Data Desktop;&lt;/li&gt;
&lt;li&gt;Finalmente no &lt;i&gt;Form Designer&lt;/i&gt;, um novo item é registrado no menu local que pode ser usado para aplicar o dicionário de dados a um &lt;i&gt;TDataset&lt;/i&gt;&amp;nbsp;- o item de menu &lt;i&gt;Data Dictionary&lt;/i&gt;.&lt;/li&gt;
&lt;/ol&gt;Vejamos então como utilizar o Lazarus Data Desktop.&lt;br /&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;Conectando-se a bancos de dados&lt;/b&gt;&lt;/h2&gt;
Para fazer uma conexão use a opção de menu &lt;i&gt;Connections &amp;gt; New connection&lt;/i&gt;, como mostra a figura abaixo:&lt;div&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiCmYKfJaUP9tbmr5BHOwG7Dy-0FyihlR0WJpRTxb2xe7KNWC7Ub1EEJmNs3mqhpmiSniuDwUl0ZuBg7NASTlF0pGQzrQMO96TVbvGkRr8bbe-dpNed59T7idsGKs-K7h6zMoYq/s1600/datadesktop.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;430&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiCmYKfJaUP9tbmr5BHOwG7Dy-0FyihlR0WJpRTxb2xe7KNWC7Ub1EEJmNs3mqhpmiSniuDwUl0ZuBg7NASTlF0pGQzrQMO96TVbvGkRr8bbe-dpNed59T7idsGKs-K7h6zMoYq/w640-h430/datadesktop.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Esta ação abrirá uma caixa de diálogo onde você irá escolher o banco de dados:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguzDxZixtbc3wsBdUGi_fqx_B6Ji79pfH7LEwQD2_Khsq0Ufz2G4L6Xe5Ue2v5vvJga-yPFGr2IYCUzpciOL8CBEOfDCS9KybT5SiH7Ia0WFG_jElUgZEEjadaCCbhRRbjubUf/s1600/selecionar+bd.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;508&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguzDxZixtbc3wsBdUGi_fqx_B6Ji79pfH7LEwQD2_Khsq0Ufz2G4L6Xe5Ue2v5vvJga-yPFGr2IYCUzpciOL8CBEOfDCS9KybT5SiH7Ia0WFG_jElUgZEEjadaCCbhRRbjubUf/w640-h508/selecionar+bd.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Selecione o banco de dados e você será levado a um novo diálogo para informar os dados da conexão:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2CQh5Xa_a1QIpiVCOtxRZNEnewNW-LGV9oX8yTMcom0Fj7u3z0oAaZjClj8L1An28aeXi55SzyTIqsCW6b3qQ4OUQKgkWOQrlmN-HCeL06QGLRFyP4S1XjXom-bjqT7NJwowW/s1600/dadosconex%25C3%25A3o.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;392&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2CQh5Xa_a1QIpiVCOtxRZNEnewNW-LGV9oX8yTMcom0Fj7u3z0oAaZjClj8L1An28aeXi55SzyTIqsCW6b3qQ4OUQKgkWOQrlmN-HCeL06QGLRFyP4S1XjXom-bjqT7NJwowW/w640-h392/dadosconex%25C3%25A3o.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Após informar os dados e confirmar será solicitado um nome para a conexão. Confirme também, após informar o nome e será adicionada uma nova aba ao ambiente com as informações da conexão. No lado direito da tela existe a aba &lt;i&gt;Run Query&lt;/i&gt;, &amp;nbsp;que possibilita executar uma consulta e exportar o resultado da consulta, entre outros recursos.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjSA6GRDNShDa4fkTvEbkoX1brDcaTiVGXNWbK3C30-cK7PcnV0Kw_wN2bt56h18OW6o7J3-PbaYewyEI8_ysIW8d7xXfUIKLC-JKxSgnQ7xr0ytLsawZizwS5lNmeZEoIsenW1/s1600/query.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;430&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjSA6GRDNShDa4fkTvEbkoX1brDcaTiVGXNWbK3C30-cK7PcnV0Kw_wN2bt56h18OW6o7J3-PbaYewyEI8_ysIW8d7xXfUIKLC-JKxSgnQ7xr0ytLsawZizwS5lNmeZEoIsenW1/w640-h430/query.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;h2 style=&quot;clear: both; text-align: left;&quot;&gt;&lt;b&gt;Exportando dados de uma tabela&lt;/b&gt;&lt;/h2&gt;Para exportar os dados resultantes de uma consulta, use o botão &lt;i&gt;Export this data&lt;/i&gt; que será habilitado após a execução bem sucedida de uma &lt;i&gt;Query&lt;/i&gt;. Em seguida escolha o tipo de arquivo, por exemplo CSV, e confirme. Então será mostrado um diálogo onde se pode escolher os campos da tabela a serem exportados, o caminho e o nome do arquivo destino e opções de formatação, conforme pode ser visto na figura a seguir.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiR8vNUpz5IJpjdaQPyKBTKBAzHru4GVDqwovIbaDVErqnj19xkPf7PNeHhqMKc8cw8RNLOzcwQ4BCQnz05GeBXDaH22Dr2hSSg4hwOejPBmVIdiaYnFicL7GRD554MdbGG-WcH/s1600/exportar+dados.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;514&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiR8vNUpz5IJpjdaQPyKBTKBAzHru4GVDqwovIbaDVErqnj19xkPf7PNeHhqMKc8cw8RNLOzcwQ4BCQnz05GeBXDaH22Dr2hSSg4hwOejPBmVIdiaYnFicL7GRD554MdbGG-WcH/w640-h514/exportar+dados.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Depois de confirmar o arquivo exportado será criado.&lt;br /&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;Gerando código Free Pascal&lt;/b&gt;&lt;/h2&gt;
Outro recurso importante do Lazarus Data Desktop é a criação de código em Free Pascal. Pode-se gerar código quando estamos visualizando o resultado de uma &lt;i&gt;Query &lt;/i&gt;ou quando estamos examinando a definição de uma tabla no dicionário de dados.&lt;br /&gt;
Os códigos que podem ser gerados são:&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;Uma constante string a partir do comando SQL quando se está visualizando o resultado de uma consulta;&lt;/li&gt;
&lt;li&gt;O código para criar uma tabela DBF a partir dos dados que estão sendo mostrados;&lt;/li&gt;
&lt;li&gt;Uma classe Object Pascal e suas propriedades baseados em um &lt;i&gt;Dataset &lt;/i&gt;e seus campos;&lt;/li&gt;
&lt;li&gt;Uma declaração de classe tiOPF para usar com Object Persistence Framework.&lt;/li&gt;
&lt;/ul&gt;Por exemplo, na aba &lt;i&gt;Run Query&lt;/i&gt;, após executar uma consulta, clique no botão &lt;i&gt;Create pascal code for this data&lt;/i&gt;. Escolha em seguida a opção &lt;i&gt;Simple object/collection for the data&lt;/i&gt;. Será aberto o seguinte diálogo:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-y8coYNFrMquOfXYrIyK_R2GWe_sn6acdkZnQijtqPH4XDc63VWkuiK3Xy0izGdFeeMG3p3gn08lvic6CsEqVoEbmat_gjsJItEKeD0av2wvUCJ0VLhozu1W6seDDwGeV-IjM/s1600/gera+codigo.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;540&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-y8coYNFrMquOfXYrIyK_R2GWe_sn6acdkZnQijtqPH4XDc63VWkuiK3Xy0izGdFeeMG3p3gn08lvic6CsEqVoEbmat_gjsJItEKeD0av2wvUCJ0VLhozu1W6seDDwGeV-IjM/w640-h540/gera+codigo.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Nessa tela você define o caminho e nome do arquivo onde deseja salvar o código e os campos da tabela que serão propriedades da classe. Para cada campo da tabela é possível definir propriedades, bem como definir algumas propriedades da própria classe na aba &lt;i&gt;Options&lt;/i&gt;. Confirme e uma tela com o código gerado será aberta, conforme mostra a figura seguinte:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTnGA9TbimGYJusYyXGDoK_KFHpb_67DACr8LbXeqgfCNXsKgpssLe8t9TjeQ-vUuxaQovSt6GtPtl9cFm_1NwxF8-otNkXyzkoOaJiZtIIFeZo-BriXbENnDmSoLrkVqV9efO/s1600/codigo+gerado.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;540&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTnGA9TbimGYJusYyXGDoK_KFHpb_67DACr8LbXeqgfCNXsKgpssLe8t9TjeQ-vUuxaQovSt6GtPtl9cFm_1NwxF8-otNkXyzkoOaJiZtIIFeZo-BriXbENnDmSoLrkVqV9efO/w640-h540/codigo+gerado.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Apesar de ter um botão &lt;i&gt;Save&lt;/i&gt;, isso não será mais necessário, pois o código já está salvo no arquivo informado na tela anterior.&lt;br /&gt;
No próximo post veremos os procedimentos para criar dicionários de dados no Lazarus Data Desktop e como utilizá-los em nossas aplicações de banco de dados.&lt;br /&gt;
Post escrito tendo como referência o livro &lt;b&gt;Lazarus - the Complete Guide&lt;/b&gt;, publicado pela &lt;a href=&quot;http://www.blaisepascal.eu/index.php?actie=./subscribers/lazarusbookinfoEnglish&quot;&gt;Blaise Pascal Magazine&lt;/a&gt;.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://professorcarlos.blogspot.com/feeds/1919385246213018284/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/33774862/1919385246213018284?isPopup=true' title='2 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/1919385246213018284'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/1919385246213018284'/><link rel='alternate' type='text/html' href='http://professorcarlos.blogspot.com/2011/07/lazarus-o-lazarus-data-desktop.html' title='Lazarus - O Lazarus Data Desktop'/><author><name>Professor Carlos</name><uri>http://www.blogger.com/profile/10347668022241306511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFBZD8558AE67R-CINkytW3INbUINaAhDuZ7GKm_4DSSKeHSpdm3K-TPQrhNPn5B-21wN24ZH8flimqC7ikTzmRrYZBAsyD_JSC8YW8BDJZwmTby9R7LZC9SYkyHkSs7NPVZxXiqEDqjhkRJNxZwqqyZaVgYjISiFpzXsSyGZmL1YFCQ/s220/WhatsApp%20Image%202024-05-17%20at%2017.42.05.jpeg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiCmYKfJaUP9tbmr5BHOwG7Dy-0FyihlR0WJpRTxb2xe7KNWC7Ub1EEJmNs3mqhpmiSniuDwUl0ZuBg7NASTlF0pGQzrQMO96TVbvGkRr8bbe-dpNed59T7idsGKs-K7h6zMoYq/s72-w640-h430-c/datadesktop.png" height="72" width="72"/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-33774862.post-5141469444839786178</id><published>2011-06-01T11:42:00.006-03:00</published><updated>2025-11-27T16:16:13.996-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="estruturas de dados"/><category scheme="http://www.blogger.com/atom/ns#" term="Free Pascal"/><category scheme="http://www.blogger.com/atom/ns#" term="Lazarus"/><category scheme="http://www.blogger.com/atom/ns#" term="TList"/><title type='text'>Free Pascal - Usando TList</title><content type='html'>&lt;h1 style=&quot;text-align: left;&quot;&gt;Free Pascal - Usando TList&lt;/h1&gt;A classe &lt;b&gt;TList &lt;/b&gt;é usada para gerenciar uma coleção de ponteiros, com a grande vantagem de ser dinâmica. Ela dispõe de métodos para fazer busca na lista e ordenar os elementos, entre outros. No exemplo abaixo fazemos uso de alguns desses métodos.&lt;br /&gt;&lt;br /&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;unit&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; Unit1&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: #6272a4;&quot;&gt;{$mode objfpc}{$H+}&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;interface&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;uses&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  Classes&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; SysUtils&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; FileUtil&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; Forms&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; Controls&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; Graphics&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; Dialogs&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; StdCtrls&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;type&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  TContato &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;class&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;private&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;      NomeContato     &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;String;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;      TelefoneContato &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;String;&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;public&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;      &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;property&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; Nome &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;String&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;          &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;read&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;NomeContato&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;      &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;property&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; Telefone &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;String&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;          &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;read&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;TelefoneContato&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;      &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;constructor&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;Create&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; pNomeContato       &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;String;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;                         &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; pTelefoneContato   &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;{ TfrmTlist }&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  TfrmTlist &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(TForm)&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    Button1&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TButton&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    ListBox1&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TListBox&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;procedure&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;Button1Click&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(Sender&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd;&quot;&gt;TObject&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;private&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;{ private declarations }&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    Agenda &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TList&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;procedure&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;MostraAgenda&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;public&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;{ public declarations }&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;var&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  frmTlist&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TfrmTlist&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;implementation&lt;/span&gt;

&lt;span style=&quot;color: #6272a4;&quot;&gt;{$R *.lfm}&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;constructor&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;TContato&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;Create&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; pNomeContato      &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;String;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;                             &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; pTelefoneContato &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;self.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;NomeContato     &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; pNomeContato&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;self.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;TelefoneContato &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; pTelefoneContato&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;comparaPorNome&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(Item1 &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd;&quot;&gt;Pointer&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; Item2 &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd;&quot;&gt;Pointer&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd;&quot;&gt;Integer&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;var&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  contato1&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; contato2 &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TContato&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  contato1 &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TContato(Item1)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  contato2 &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TContato(Item2)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;      contato1&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Nome &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; contato2&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Nome&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;then&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #f8f8f2; font-style: italic;&quot;&gt;Result&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;1&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; contato1&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Nome &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; contato2&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Nome&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;then&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #f8f8f2; font-style: italic;&quot;&gt;Result&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #f8f8f2; font-style: italic;&quot;&gt;Result&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;procedure&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;TfrmTlist&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;Button1Click&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(Sender&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd;&quot;&gt;TObject&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;var&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  contato &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TContato&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  Agenda &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TList&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Create&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  contato &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TContato&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Create(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Nádia Alves&#39;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;3523-0001&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  Agenda&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Add(contato)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  contato &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TContato&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Create(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Célio Silva&#39;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;3524-0000&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  Agenda&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Add(contato)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  Agenda&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Add(TContato&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Create(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Hélio Costa&#39;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;3320-1000&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;))&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  Agenda&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Add(TContato&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Create(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Aldo Sousa&#39;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;3321-0001&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;))&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  MostraAgenda&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  ShowMessage(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Ordenando pelo nome. Pressione OK&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  Agenda&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Sort(&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;comparaPorNome)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  MostraAgenda&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  ShowMessage(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Inserindo novo contato. Pressione OK&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  Agenda&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Insert(&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TContato&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Create(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Luis Gonzaga&#39;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;3325-4000&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;))&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  MostraAgenda&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  ShowMessage(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Excluindo um contato. Pressione OK&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  Agenda&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Delete(&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  ShowMessage(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Pressione OK&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  Agenda&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;free&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;procedure&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;TfrmTlist&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;MostraAgenda&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;var&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  i &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd;&quot;&gt;Integer&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  ListBox1&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Items&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Clear&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;to&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; Agenda&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Count&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;do&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    ListBox1&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Items&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Add(TContato(Agenda[i])&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Nome&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39; - &#39;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;+&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;                      (TContato(Agenda[i])&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Telefone))&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;end.&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;br /&gt;
No exemplo criamos uma classe &lt;i&gt;Contato &lt;/i&gt;que será usada para povoar um &lt;b&gt;TList&lt;/b&gt;&amp;nbsp;que denominamos &lt;i&gt;Agenda&lt;/i&gt;. Um &lt;b&gt;TListBox &lt;/b&gt;é usado para apresentar os dados do &lt;b&gt;TList&lt;/b&gt;.&lt;br /&gt;
Nas linhas que vão de 74 a 95 é onde acontece tudo que pretendemos mostrar. Inicialmente o &lt;b&gt;TList &lt;/b&gt;é criado. Em seguida, são criados vários objetos &lt;i&gt;Contato&lt;/i&gt;, que são adicionados à &lt;i&gt;Agenda &lt;/i&gt;utilizando o método &lt;b&gt;Add()&lt;/b&gt;. Este método sempre adiciona um objeto no fim da lista.&lt;br /&gt;
Após isso chamamos o método &lt;i&gt;MostraAgenda&lt;/i&gt;, que foi criado para visualizar os dados da &lt;i&gt;Agenda&lt;/i&gt; no &lt;b&gt;TListBox&lt;/b&gt;. Na linha 86 há uma chamada ao método &lt;b&gt;Sort()&lt;/b&gt;, que recebe como argumento o método &lt;b&gt;comparaPorNome()&lt;/b&gt;. Aqui cabe uma explicação mais detalhada.&lt;br /&gt;
A ordenação de uma lista de objetos não é uma coisa tão natural como ordenar números inteiros ou strings. Como sabemos um objeto tem vários campos e por isso precisamos definir qual o critério que será usado para decidir que um objeto A é maior, menor ou igual a um outro objeto B. Por este motivo o método &lt;b&gt;Sort()&lt;/b&gt; deve receber como parâmetro uma variável procedural, ou seja, um método que estabelece o critério de ordenação.&lt;br /&gt;
Dessa forma, foi criado o método &lt;b&gt;comparaPorNome()&lt;/b&gt;, que recebe os ponteiros dos dois objetos a serem comparados. Tal método deve retornar -1 se o primeiro objeto for menor que o segundo, 1 se for maior e 0 se forem iguais. No nosso exemplo definimos que dois &lt;i&gt;Contatos &lt;/i&gt;são iguais quando tem nomes iguais. Leia mais sobre variáveis procedurais no post &lt;a href=&quot;https://professorcarlos.blogspot.com/2011/05/free-pascal-variaveis-procedurais.html&quot;&gt;Free Pascal - Variáveis Procedurais&lt;/a&gt;. Deve-se ressaltar que o Free Pascal define apenas como deve ser a assinatura do método que ele espera receber em &lt;b&gt;Sort()&lt;/b&gt;. A implementação cabe ao desenvolvedor.&lt;br /&gt;
Na linha 90 um novo objeto é inserido na posição 2 do &lt;b&gt;TList&lt;/b&gt;. Note que a primeira posição é 0.&lt;br /&gt;
Usamos o método &lt;b&gt;Delete()&lt;/b&gt; para deletar um objeto em uma determinada posição. Na linha 93 excluímos o objeto da posição 4.&lt;br /&gt;
Quando a &lt;b&gt;TList &lt;/b&gt;não é mais necessária, liberamos a memória usada chamando &lt;b&gt;Free&lt;/b&gt;. Para criar uma lista de strings prefira usar &lt;b&gt;TStrings &lt;/b&gt;ou sua descendente &lt;b&gt;TStringList&lt;/b&gt;.</content><link rel='replies' type='application/atom+xml' href='http://professorcarlos.blogspot.com/feeds/5141469444839786178/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/33774862/5141469444839786178?isPopup=true' title='8 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/5141469444839786178'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/5141469444839786178'/><link rel='alternate' type='text/html' href='http://professorcarlos.blogspot.com/2011/06/free-pascal-usando-tlist.html' title='Free Pascal - Usando TList'/><author><name>Professor Carlos</name><uri>http://www.blogger.com/profile/10347668022241306511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFBZD8558AE67R-CINkytW3INbUINaAhDuZ7GKm_4DSSKeHSpdm3K-TPQrhNPn5B-21wN24ZH8flimqC7ikTzmRrYZBAsyD_JSC8YW8BDJZwmTby9R7LZC9SYkyHkSs7NPVZxXiqEDqjhkRJNxZwqqyZaVgYjISiFpzXsSyGZmL1YFCQ/s220/WhatsApp%20Image%202024-05-17%20at%2017.42.05.jpeg'/></author><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-33774862.post-7586656266766624213</id><published>2011-05-15T17:22:00.008-03:00</published><updated>2025-11-27T08:15:29.640-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="banco de dados"/><category scheme="http://www.blogger.com/atom/ns#" term="Lazarus"/><category scheme="http://www.blogger.com/atom/ns#" term="SQLdb"/><title type='text'>Lazarus - Acessando banco de dados com SQLdb - Parte III</title><content type='html'>&lt;h1 style=&quot;text-align: left;&quot;&gt;Lazarus - Acessando banco de dados com SQLdb - Parte III&lt;/h1&gt;Muita coisa mudou no Lazarus desde que começamos a dedicar este blog à divulgação deste poderoso IDE. Quando iniciamos usávamos a versão 0.9.28 e publicamos dois artigos voltados para o uso do conjunto de componentes &lt;b&gt;SQLdb&lt;/b&gt;:&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;https://professorcarlos.blogspot.com/2010/02/lazarus-acessando-banco-de-dados-com.html&quot;&gt;Lazarus - Acessando banco de dados com SQLdb - Parte I&lt;/a&gt; &lt;br /&gt;
&lt;a href=&quot;https://professorcarlos.blogspot.com/2010/02/lazarus-acessando-banco-de-dados-com_24.html&quot;&gt;Lazarus - Acessando banco de dados com SQLdb - Parte II&lt;/a&gt; &lt;br /&gt;
&lt;br /&gt;
Hoje estamos com a versão 0.9.31. Neste post iremos apresentar algumas mudanças para melhor na utilização desses componentes.&lt;br /&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;Referências aos campos de uma tabela - criação de TFields&lt;/b&gt;&lt;/h2&gt;
Para criar os &lt;b&gt;TFields&lt;/b&gt; ainda é necessário que a &lt;b&gt;TSQLQuery&lt;/b&gt; esteja ativa. Dê um duplo clique na &lt;b&gt;TSQLQuery&lt;/b&gt; e você verá uma janela como esta:&lt;div&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhruHCQ09NOnPPxBGTyEQJJu0vcYI6ATw0mXiF46ENyujwM-dYG4DM-NgDfUtqMNUXfpP1w5ICDgaVsB-RotUjzOP89euSC45AxFMSDMCv_efnvNYZ3OCCqoh-4qbo4J-y4tWVb/s1600/editfields.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;400&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhruHCQ09NOnPPxBGTyEQJJu0vcYI6ATw0mXiF46ENyujwM-dYG4DM-NgDfUtqMNUXfpP1w5ICDgaVsB-RotUjzOP89euSC45AxFMSDMCv_efnvNYZ3OCCqoh-4qbo4J-y4tWVb/w293-h400/editfields.png&quot; width=&quot;293&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
Esta janela oferece uma barra de ferramentas bastante intuitiva. O botão adicionar irá abrir uma nova janela onde você poderá selecionar os campos da tabela que se deseja adicionar. Após selecionar os campos pressione o botão &lt;b&gt;Create&lt;/b&gt;. O botão novo campo permite criar um campo de dado, calculado ou &lt;i&gt;lookup&lt;/i&gt;. O uso desse botão irá abrir a seguinte janela:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDgb5jCkAQVexksF66iy8bB5GaC2NqR-yd6614a95qR-S9f3v8Og5BdxmAOFJJzhLgdmGxEdPLaTiJgWjLMcE4YNqlMQllG4J0BguqaYDd2fLRepWUDE_HSDV2hyphenhyphenEoEGcCrDmG/s1600/newfield.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;353&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiDgb5jCkAQVexksF66iy8bB5GaC2NqR-yd6614a95qR-S9f3v8Og5BdxmAOFJJzhLgdmGxEdPLaTiJgWjLMcE4YNqlMQllG4J0BguqaYDd2fLRepWUDE_HSDV2hyphenhyphenEoEGcCrDmG/w400-h353/newfield.png&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Aqui você escolhe o tipo do campo em &lt;b&gt;Field Type&lt;/b&gt;. Em &lt;b&gt;Field properties&lt;/b&gt; podemos definir os valores das propriedades do campo. Depois confirme tudo pressionando &lt;b&gt;Ok&lt;/b&gt;. Esteja sempre atento que as tabelas envolvidas na criação dos campos devem estar abertas.&lt;br /&gt;
Note também que esse procedimento irá criar variáveis do tipo &lt;b&gt;TField&lt;/b&gt;, que podem ser usadas no código do nosso programa. Essas variáveis são criadas abaixo do &lt;b&gt;Data Module&lt;/b&gt;&amp;nbsp;conforme podemos ver na figura:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhczxezHm9ydn8Rb_nB5cXju8LGzPs58osSKdcltE6JTLN4E0A7EB9Ao_nrzcBDbqqTqubLbHPHqqObey9wW7LRDc08hrOnJ8TBHxVt3ipYP-qPN1pXvLtCqKt_Tvdv1TwtglfP/s1600/tfields.png&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;453&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhczxezHm9ydn8Rb_nB5cXju8LGzPs58osSKdcltE6JTLN4E0A7EB9Ao_nrzcBDbqqTqubLbHPHqqObey9wW7LRDc08hrOnJ8TBHxVt3ipYP-qPN1pXvLtCqKt_Tvdv1TwtglfP/w640-h453/tfields.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
A partir disso, podemos acessar os campos da tabela usando essas variáveis, lembrando que elas são objetos. Veja alguns exemplos de utilização desses objetos:&lt;br /&gt;
&lt;br /&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;queCidadeidcidade&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Value&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;// lê o valor do campo, inteiro nesse caso&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;queCidadeidcidade&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;AsString&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;// lê o valor do campo convertido para string&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;br /&gt;
Criação de &lt;b&gt;TFields&lt;/b&gt; não é uma novidade. Isso já existia nas versões anteriores. Apenas ficou um pouco mais intuitivo e fácil manipular a criação desses objetos.&lt;br /&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;Referência aos parâmetros de um comando SQL&lt;/b&gt;&lt;/h2&gt;
Como sabemos, é possível escrever comandos SQL utilizando parâmetros. Um exemplo é mostrado no código abaixo:&lt;br /&gt;
&lt;br /&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;procedure&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;TfrmConsCidade&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;btnPesquisarClick&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(Sender&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd;&quot;&gt;TObject&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;with&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; dmDados&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;queConsCidade &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;do&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;Close&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    SQL&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Clear&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; (rgCampo&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;ItemIndex &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;then&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;      &lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;// pidcidade é um parâmetro&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;      SQL&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Add(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;select * from cidade where idcidade = :pidcidade&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;      &lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;// atribuição do valor do parâmetro&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;      ParamByName(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;pidcidade&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Value &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;StrToInt&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(edValor&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Text)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;      &lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;//Params[0].Value := StrToInt(edValor.Text);&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;end&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;else&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;      SQL&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Add(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;select * from cidade where nome like :pnome&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;      ParamByName(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;pnome&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Value &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; edValor&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Text &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;%&#39;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    Open&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;br /&gt;
Em versões anteriores não havia o método &lt;b&gt;ParamByName()&lt;/b&gt;. O acesso a um parâmetro era feito usando &lt;b&gt;Params[]&lt;/b&gt;. Isso é mostrado na linha de código 13, que aparece comentada. O uso de &lt;b&gt;ParamByName()&lt;/b&gt; torna o código mais legível.&lt;br /&gt;
O &lt;b&gt;SQLdb&lt;/b&gt; melhorou muito, mas ainda aconselhamos o uso de &lt;b&gt;ZeosLib&lt;/b&gt;.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://professorcarlos.blogspot.com/feeds/7586656266766624213/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/33774862/7586656266766624213?isPopup=true' title='8 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/7586656266766624213'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/7586656266766624213'/><link rel='alternate' type='text/html' href='http://professorcarlos.blogspot.com/2011/05/lazarus-banco-de-dados-sqldb-parte-iii.html' title='Lazarus - Acessando banco de dados com SQLdb - Parte III'/><author><name>Professor Carlos</name><uri>http://www.blogger.com/profile/10347668022241306511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFBZD8558AE67R-CINkytW3INbUINaAhDuZ7GKm_4DSSKeHSpdm3K-TPQrhNPn5B-21wN24ZH8flimqC7ikTzmRrYZBAsyD_JSC8YW8BDJZwmTby9R7LZC9SYkyHkSs7NPVZxXiqEDqjhkRJNxZwqqyZaVgYjISiFpzXsSyGZmL1YFCQ/s220/WhatsApp%20Image%202024-05-17%20at%2017.42.05.jpeg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhruHCQ09NOnPPxBGTyEQJJu0vcYI6ATw0mXiF46ENyujwM-dYG4DM-NgDfUtqMNUXfpP1w5ICDgaVsB-RotUjzOP89euSC45AxFMSDMCv_efnvNYZ3OCCqoh-4qbo4J-y4tWVb/s72-w293-h400-c/editfields.png" height="72" width="72"/><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-33774862.post-8632205520283068613</id><published>2011-05-02T00:24:00.008-03:00</published><updated>2025-11-27T18:45:22.398-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="aplicação console"/><category scheme="http://www.blogger.com/atom/ns#" term="Lazarus"/><category scheme="http://www.blogger.com/atom/ns#" term="tipos procedurais"/><title type='text'>Free Pascal - Variáveis Procedurais</title><content type='html'>&lt;h1 style=&quot;text-align: left;&quot;&gt;Free Pascal - Variáveis Procedurais&lt;/h1&gt;Depois de um longo tempo afastado do blog, estamos retornando. Iremos iniciar uma série de artigos dedicados às estruturas de dados do Free Pascal. Mas antes de iniciar é necessário uma breve introdução a variáveis procedurais. &lt;br /&gt;
Variáveis procedurais ou tipos procedurais são um recurso do Free Pascal que permite armazenar métodos, funções e procedimentos em variáveis. Tais variáveis podem ser normalmente tratadas, passando-as como parâmetros e chamando os métodos quando for necessário.&lt;br /&gt;
Para exemplificar o uso deste recurso vejamos como declarar um tipo procedural:&lt;br /&gt;
&lt;br /&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;type&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  TFuncaoSemParametro &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;String;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  TFuncaoComParametro &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(x&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;String;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;br /&gt;
O primeiro exemplo declara um tipo que armazena um função que não recebe argumentos. O segundo exemplo declara um tipo que armazena uma função que recebe parâmetros.&lt;br /&gt;
Tipos procedurais também podem ser declarados de mais duas maneiras, como pode ser visto abaixo. O primeiro declara um tipo para armazenar um procedimento e o segundo armazena um método de uma classe:&lt;br /&gt;
&lt;br /&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;type&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;   TProcedimento &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;procedure;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;TMetodo&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;procedure&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;of&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;Object;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;br /&gt;
Prosseguindo então com nosso exemplo, em seguida iremos declarar as funções:&lt;br /&gt;
&lt;br /&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;AloMundo&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;String;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #f8f8f2; font-style: italic;&quot;&gt;Result&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Alo Mundo&#39;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;AloNovamente&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(S&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;String;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #f8f8f2; font-style: italic;&quot;&gt;Result&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Alo &#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; S&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;br /&gt;
Essas duas funções serão usadas como argumentos nas chamadas aos dois procedimentos seguintes, que recebem como parâmetros os tipos procedurais declarados.&lt;br /&gt;
&lt;br /&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;procedure&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;Primeiro&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(f&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TFuncaoSemParametro)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;WriteLn&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(f())&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;procedure&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;Segundo&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(f&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TFuncaoComParametro)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;WriteLn&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(f(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Novamente&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;))&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;           &lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;br /&gt;
Observe que tanto o procedimento Primeiro, quanto o procedimento Segundo executam a função que recebem como parâmetro. As funções chamadas irão retornar Strings que serão escritas no console através de &lt;b&gt;writeln&lt;/b&gt;.&lt;br /&gt;
Para testar a funcionalidade apenas chame os procedimentos assim:&lt;br /&gt;
&lt;br /&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Primeiro(&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;AloMundo)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Segundo(&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;AloNovamente)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;br /&gt;
O símbolo @ converte a variável em um ponteiro. É desta forma que a função é passada como parâmetro.&lt;br /&gt;
É importante observar que o compilador  verifica se a função ou o procedimento que está sendo passado como parâmetro corresponde exatamente  ao esperado, ou seja, possui os mesmos parâmetros, o mesmo tipo de  retorno, etc.&lt;br /&gt;
O mecanismo usado pelos eventos de componentes da biblioteca do Lazarus é exatamente este. Veja como pode ser criado um componente dinamicamente e como atribuir um método a um evento de um &lt;i&gt;TButton&lt;/i&gt; no post &lt;a href=&quot;http://professorcarlos.blogspot.com/2010/05/lazarus-componentes-em-run-time.html&quot;&gt;Lazarus - Criando componentes em run-time&lt;/a&gt;.&lt;br /&gt;
A seguir o código completo da aplicação console:&lt;br /&gt;
&lt;br /&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;program&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; procvar&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: #6272a4;&quot;&gt;{$mode objfpc}{$H+}&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;uses&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;{$IFDEF UNIX}{$IFDEF UseCThreads}&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  cthreads&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;,&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;{$ENDIF}{$ENDIF}&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  Classes&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;{ you can add units after this }&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;type&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  TFuncaoSemParametro &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;String;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  TFuncaoComParametro &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(x&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;String;&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;AloMundo&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;String;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #f8f8f2; font-style: italic;&quot;&gt;Result&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Alo Mundo&#39;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;AloNovamente&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(S&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;String;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #f8f8f2; font-style: italic;&quot;&gt;Result&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Alo &#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; S&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;procedure&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;Primeiro&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(f&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TFuncaoSemParametro)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;WriteLn&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(f())&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;procedure&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;Segundo&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(f&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TFuncaoComParametro)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;WriteLn&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(f(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Novamente&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;))&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;

&lt;span style=&quot;color: #6272a4;&quot;&gt;{$R *.res}&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  Primeiro(&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;AloMundo)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  Segundo(&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;@&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;AloNovamente)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;ReadLn&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;end.&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div style=&quot;color: white;&quot;&gt;&lt;span style=&quot;background-color: red;&quot;&gt;Atualizado em 03/05/2011.&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://professorcarlos.blogspot.com/feeds/8632205520283068613/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/33774862/8632205520283068613?isPopup=true' title='2 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/8632205520283068613'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/8632205520283068613'/><link rel='alternate' type='text/html' href='http://professorcarlos.blogspot.com/2011/05/free-pascal-variaveis-procedurais.html' title='Free Pascal - Variáveis Procedurais'/><author><name>Professor Carlos</name><uri>http://www.blogger.com/profile/10347668022241306511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFBZD8558AE67R-CINkytW3INbUINaAhDuZ7GKm_4DSSKeHSpdm3K-TPQrhNPn5B-21wN24ZH8flimqC7ikTzmRrYZBAsyD_JSC8YW8BDJZwmTby9R7LZC9SYkyHkSs7NPVZxXiqEDqjhkRJNxZwqqyZaVgYjISiFpzXsSyGZmL1YFCQ/s220/WhatsApp%20Image%202024-05-17%20at%2017.42.05.jpeg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-33774862.post-5037324380082143991</id><published>2010-10-07T17:45:00.008-03:00</published><updated>2025-11-27T18:37:34.600-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="form dinâmico"/><category scheme="http://www.blogger.com/atom/ns#" term="herança"/><category scheme="http://www.blogger.com/atom/ns#" term="Lazarus"/><category scheme="http://www.blogger.com/atom/ns#" term="ZeosLib"/><title type='text'>Lazarus – Herança de form e criação de componentes em run-time</title><content type='html'>&lt;h1 style=&quot;text-align: left;&quot;&gt;Lazarus – Herança de form e criação de componentes em run-time&lt;/h1&gt;Neste post vamos mostrar como criar um formulário de cadastro, independente da  tabela do banco de dados. A idéia é que, ao abrir o formulário, um trecho de  código identifica cada campo da tabela, instancia um TDBEdit para esse campo e  ainda o identifica com um TLabel. Além do mais esse código será implementado em  um formulário pai. Cada formulário de cadastro será derivado – ou herdado –  deste. Nosso demo terá um form principal que chamará o form de cadastro, o form  pai onde todas as ações irão acontecer, um form de cadastro derivado e um  &lt;b&gt;Data Module&lt;/b&gt;.&lt;br /&gt;
No &lt;b&gt;Data Module&lt;/b&gt; colocamos os componentes de acesso ao banco de  dados. Para isso iremos utilizar &lt;b&gt;ZeosLib&lt;/b&gt; com banco de dados  PostgreSQL. Coloque então um &lt;b&gt;TZConnection&lt;/b&gt; para conectar ao seu  banco de dados e o configure adequadamente. Coloque também uma  &lt;b&gt;TZQuery&lt;/b&gt; e um &lt;b&gt;TZUpdateSQL&lt;/b&gt;. Consulte o post &lt;a href=&quot;https://professorcarlos.blogspot.com/2010/03/lazarus-conectando-postgresql-com.html&quot; target=&quot;_blank&quot;&gt;Lazarus – Conectando PostgreSQL com ZeosLib&lt;/a&gt; se tiver dúvida de  como fazer essas definições. Não coloque &lt;b&gt;TDataSource&lt;/b&gt; no  &lt;b&gt;Data Module&lt;/b&gt;. Iremos colocá-lo no form pai.&lt;br /&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;O form Pai&lt;/b&gt;&lt;/h2&gt;
Já existe um form na aplicação. Vamos inserir um novo form e deixar esse primeiro para ser o principal. No novo form vamos criar a propriedade &lt;b&gt;Query&lt;/b&gt; conforme mostra o código abaixo. Inclua a unit &lt;b&gt;ZDataset&lt;/b&gt; na cláusula &lt;b&gt;uses&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;private&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;{ private declarations }&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    fQuery&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TZQuery&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;GetQuery&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TZQuery&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;procedure&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;SetQuery&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(NewQuery&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TZQuery)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;public&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #6272a4;&quot;&gt;{ public declarations }&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;property&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; Query&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TZQuery &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;read&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;GetQuery&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;write&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;SetQuery&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;var&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  frmDinamico&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TfrmDinamico&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  vetorCampos&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;of&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TDBEdit&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  vetorLabel&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;of&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TLabel&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;implementation&lt;/span&gt;

&lt;span style=&quot;color: #6272a4;&quot;&gt;{$R *.lfm}&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;procedure&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;TfrmDinamico&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;FormShow&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(Sender&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd;&quot;&gt;TObject&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;var&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;   Coluna &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd;&quot;&gt;integer&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;   NomeColuna &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TDBEdit&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;   NomeLabel &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TLabel&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;   Topo &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd;&quot;&gt;integer&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;   Topo &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;65&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;   dsTabela&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;DataSet &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; fQuery&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;   fQuery&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Open&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;SetLength&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(vetorCampos&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; fQuery&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;FieldCount)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;SetLength&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(vetorLabel&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; fQuery&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;FieldCount)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; Coluna &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;to&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; fQuery&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;FieldCount &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;do&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;     NomeColuna &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TDBEdit&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Create(&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;Self&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;     NomeColuna&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Parent &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;Self;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;     NomeColuna&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Left &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;105&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;     NomeColuna&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Top &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; Topo&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;     NomeColuna&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Width &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;15&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; fQuery&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Fields[Coluna]&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;DisplayWidth &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;     NomeColuna&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;DataSource &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; dsTabela&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;     NomeColuna&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;DataField &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; fQuery&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Fields[Coluna]&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;FieldName&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;     NomeColuna&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Tag &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; Coluna&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;     Topo &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; Topo &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;25&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;     NomeColuna&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Visible &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;True;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;     NomeColuna&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Name &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;DBEdit&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;IntToStr&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(Coluna)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;     vetorCampos[Coluna] &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; NomeColuna&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: #f8f8f2;&quot;&gt;     NomeLabel &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TLabel&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Create(&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;Self&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;     NomeLabel&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Parent &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;Self;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;     NomeLabel&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Left &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;     NomeLabel&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Top &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; NomeColuna&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Top &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; NomeLabel&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Height&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;     NomeLabel&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Caption &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; fQuery&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Fields[Coluna]&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;DisplayName&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;     NomeLabel&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Tag &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; Coluna&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;     NomeLabel&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Visible &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;True;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;     NomeLabel&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Name &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;Label&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;IntToStr&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(Coluna)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;     vetorLabel[Coluna] &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; NomeLabel&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; Topo &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;25&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;445&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;then&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;      Height &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;445&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;else&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;      Height &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; Topo &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;25&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;procedure&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;TfrmDinamico&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;FormClose&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(Sender&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd;&quot;&gt;TObject&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; CloseAction&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TCloseAction&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  )&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  fQuery&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Close&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  CloseAction &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; caFree&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;function&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;TfrmDinamico&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;GetQuery&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TZQuery&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #f8f8f2; font-style: italic;&quot;&gt;Result&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; fQuery&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;procedure&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;TfrmDinamico&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;SetQuery&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(NewQuery&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TZQuery)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  fQuery &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; NewQuery&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;br /&gt;
Implemente os métodos &lt;b&gt;GetQuery&lt;/b&gt; e &lt;b&gt;SetQuery&lt;/b&gt; conforme mostrado no código. Na seção &lt;b&gt;var&lt;/b&gt; declaramos dois arrays dinâmicos para receber os objetos &lt;b&gt;TDBEdit&lt;/b&gt; e &lt;b&gt;TLabel&lt;/b&gt;. Agora vamos analisar com mais detalhes o código do evento &lt;b&gt;OnShow&lt;/b&gt;. É nesse código que serão criados os objetos em tempo de execução, de acordo com os campos da tabela. No post &lt;a href=&quot;http://professorcarlos.blogspot.com/2010/05/lazarus-componentes-em-run-time.html&quot;&gt;Lazarus - Criando componentes em run-time&lt;/a&gt; nós mostramos como criar componentes em tempo de execução.&lt;br /&gt;
Inicialmente no método nós definimos o tamanho dos arrays de acordo com o número de campos da tabela usando o procedure &lt;b&gt;SetLength&lt;/b&gt;. Em seguida, dentro de um &lt;b&gt;for&lt;/b&gt; que percorre os campos da tabela, é criado um &lt;b&gt;TDBEdit&lt;/b&gt; e um &lt;b&gt;TLabel&lt;/b&gt; para cada campo. Cada componente é guardado em seu array correspondente. Depois disso é verificada a necessidade de se aumentar a altura do form para que ele possa mostrar todos os componentes criados. Essa verificação é feita através da variável &lt;i&gt;Topo&lt;/i&gt; que guarda a posição vertical de cada componente. Como o nosso form é dinâmico, nós liberamos a memória ocupada por ele assim que o mesmo for fechado.&lt;br /&gt;&lt;h2 style=&quot;text-align: left;&quot;&gt;&lt;b&gt;Herdando o form Pai&lt;/b&gt;&lt;/h2&gt;
Depois disso vamos criar o form de cadastro propriamente dito. Para isso selecione o menu &lt;b&gt;File | New&lt;/b&gt; e clique em &lt;b&gt;Inherited Component&lt;/b&gt;. Na caixa da direita irão aparecer os forms que podem ser herdados. Escolha o form Pai que foi criado anteriormente e pressione &lt;b&gt;OK&lt;/b&gt;. Um novo form será criado com todos os membros do form Pai. Agora selecione a opção de menu &lt;b&gt;Project | Project Options&lt;/b&gt;. Clique em &lt;b&gt;Forms&lt;/b&gt; e transfira o form Filho para a caixa da direita. Isso determina que o form Filho será instanciado em tempo de execução. Para que tudo funcione perfeitamente no form Filho, devemos implementar os métodos herdados e informar a palavra chave &lt;b&gt;inherited&lt;/b&gt;. No nosso caso precisamos implementar apenas os método dos eventos &lt;b&gt;OnShow&lt;/b&gt; e &lt;b&gt;OnClose&lt;/b&gt;, como mostra o código abaixo.&lt;br /&gt;
&lt;br /&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;procedure&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;TfrmCidade&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;FormShow&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(Sender&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd;&quot;&gt;TObject&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;inherited&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;

&lt;span style=&quot;color: #ff79c6;&quot;&gt;procedure&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;TfrmCidade&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #50fa7b;&quot;&gt;FormClose&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(Sender&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd;&quot;&gt;TObject&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;var&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; CloseAction&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TCloseAction)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;inherited&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;            &lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;br /&gt;
Para testar nosso demo, vamos ao form principal. Coloque um botão e escreva o seguinte código no evento &lt;b&gt;OnClick&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TfrmCidade(Application&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;FindComponent(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;frmCidade&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)) &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;nil&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;then&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  frmCidade &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; TfrmCidade&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Create(Application)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;frmCidade&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Query &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; dmDados&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;queCidade&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;frmCidade&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Show&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;br /&gt;
Estamos supondo que o nome do form Filho é &lt;i&gt;frmCidade&lt;/i&gt;, que o nome do &lt;b&gt;Data Module&lt;/b&gt; é &lt;i&gt;dmDados&lt;/i&gt; e que a &lt;b&gt;TZQuery&lt;/b&gt; no &lt;b&gt;Data Module&lt;/b&gt; chama-se &lt;i&gt;queCidade&lt;/i&gt;. Pronto você pode executar o programa e pressionar o botão. Você terá uma tela assim:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0RpyM56ojCu2YyDFOwsfzJD-oBPRid75AJOs1E-8hKGPN5-hCLSQK1_HZlX3EER_u9Eii9DtaOvopA8YFT0QDFbvVKzqlW0Gwq0a91yFC8V-VGPbx3K1quZc-MGlmIO7Bki23/s1600/heranca.jpg&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;356&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0RpyM56ojCu2YyDFOwsfzJD-oBPRid75AJOs1E-8hKGPN5-hCLSQK1_HZlX3EER_u9Eii9DtaOvopA8YFT0QDFbvVKzqlW0Gwq0a91yFC8V-VGPbx3K1quZc-MGlmIO7Bki23/w640-h356/heranca.jpg&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
Você pode agora repetir esse procedimento para todas as tabelas que precisem de formulários de cadastro. Bem, certamente não é um form de cadastro perfeito, pois todos os campos são definidos com &lt;b&gt;TDBEdit&lt;/b&gt;, mas a partir daqui você pode tentar vôos mais altos.</content><link rel='replies' type='application/atom+xml' href='http://professorcarlos.blogspot.com/feeds/5037324380082143991/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/33774862/5037324380082143991?isPopup=true' title='2 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/5037324380082143991'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/5037324380082143991'/><link rel='alternate' type='text/html' href='http://professorcarlos.blogspot.com/2010/10/lazarus-heranca-de-form.html' title='Lazarus – Herança de form e criação de componentes em run-time'/><author><name>Professor Carlos</name><uri>http://www.blogger.com/profile/10347668022241306511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFBZD8558AE67R-CINkytW3INbUINaAhDuZ7GKm_4DSSKeHSpdm3K-TPQrhNPn5B-21wN24ZH8flimqC7ikTzmRrYZBAsyD_JSC8YW8BDJZwmTby9R7LZC9SYkyHkSs7NPVZxXiqEDqjhkRJNxZwqqyZaVgYjISiFpzXsSyGZmL1YFCQ/s220/WhatsApp%20Image%202024-05-17%20at%2017.42.05.jpeg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0RpyM56ojCu2YyDFOwsfzJD-oBPRid75AJOs1E-8hKGPN5-hCLSQK1_HZlX3EER_u9Eii9DtaOvopA8YFT0QDFbvVKzqlW0Gwq0a91yFC8V-VGPbx3K1quZc-MGlmIO7Bki23/s72-w640-h356-c/heranca.jpg" height="72" width="72"/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-33774862.post-4593694644821961880</id><published>2010-09-27T21:04:00.012-03:00</published><updated>2025-11-27T18:41:21.205-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Lazarus"/><category scheme="http://www.blogger.com/atom/ns#" term="PostgreSQL"/><category scheme="http://www.blogger.com/atom/ns#" term="transação"/><category scheme="http://www.blogger.com/atom/ns#" term="ZeosLib"/><title type='text'>Lazarus - AutoCommit no ZeosLib com PostgreSQL</title><content type='html'>&lt;h1 style=&quot;text-align: left;&quot;&gt;Lazarus - AutoCommit no ZeosLib com PostgreSQL&lt;/h1&gt;Por padrão, o PostgreSQL trata cada comando SQL como sendo executado dentro de uma transação. Ou seja, cada comando DML (INSERT, UPDATE ou DELETE) tem um comando de início de transação (BEGIN) e um comando COMMIT – caso seja bem sucedido – executados implicitamente antes e depois. Para a execução de múltiplos comandos DML isso leva a um &lt;i&gt;overhead&lt;/i&gt;. Desta forma, sugere-se fortemente envolver todos os comandos DML em uma única transação – se a lógica da sua aplicação permitir. Para demonstrar esta recomendação fizemos um teste simples. Escrevemos um pequeno programa para inserir 5.000 linhas em uma tabela. Primeiro fizemos o teste com o controle de transação padrão - cada uma das 5.000 linhas tem seu próprio BEGIN e COMMIT - e depois modificamos o código para controlar a transação manualmente. Neste último teste todas as 5.000 linhas são inseridas dentro de uma única transação.&lt;br /&gt;
Para nosso teste usamos ZeosLib. Por padrão o ZeosLib também opera no modo Auto-Commit, ou seja, cada comando é executado no contexto de uma transação. Não mostraremos aqui como conectar ao PG usando Zeos, pois isso já foi apresentado no post &lt;a href=&quot;https://professorcarlos.blogspot.com/2010/03/lazarus-conectando-postgresql-com.html&quot;&gt;Lazarus - Conectando PostgreSQL com ZeosLib&lt;/a&gt;. Vejamos o primeiro teste. A unit &lt;b&gt;Dateutils&lt;/b&gt; deve ser incluida na cláusula &lt;b&gt;uses&lt;/b&gt; para poder usar a função &lt;br /&gt;
&lt;b&gt;MilliSecondsBetween()&lt;/b&gt;. &lt;i&gt;dbBanco&lt;/i&gt; é um &lt;b&gt;ZConnection&lt;/b&gt;, &lt;i&gt;dmDados&lt;/i&gt; é um &lt;b&gt;Data Module&lt;/b&gt; e &lt;i&gt;queCidade&lt;/i&gt; uma &lt;b&gt;ZQuery&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;var&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  i&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd;&quot;&gt;integer&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  a&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; b&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd;&quot;&gt;TDateTime&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  dmDados&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;queCidade&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;SQL&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Clear&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  dmDados&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;queCidade&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;SQL&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Add(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;insert into cidade values (:id, :nome)&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  a &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;time&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;to&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;5000&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;do&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    dmDados&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;queCidade&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;ParamByName(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;ID&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Value&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; i&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    dmDados&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;queCidade&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;ParamByName(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;NOME&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Value&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;SANTAREM&#39;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    dmDados&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;queCidade&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;ExecSql&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  b &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;time&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  ShowMessage(&lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;IntToStr&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(MilliSecondsBetween(b&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;a)))&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;br /&gt;
Neste primeiro código o tempo médio de inserção das 5.000 linhas foi de 11,6 segundos. Alteramos então nosso código para que todas as linhas fossem inseridas dentro de uma única transação. Para isso chamamos &lt;b&gt;StartTransaction&lt;/b&gt; antes de iniciar os &lt;b&gt;INSERT&lt;/b&gt;s. Observe o código abaixo.&lt;div&gt;&lt;br /&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;var&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  i&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd;&quot;&gt;integer&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  a&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; b&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd;&quot;&gt;TDateTime&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  a &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;time&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  dmDados&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;queCidade&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;SQL&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Clear&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  dmDados&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;queCidade&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;SQL&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Add(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;insert into cidade values (:id, :nome)&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  dmDados&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;dbBanco&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;StartTransaction&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;to&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;5000&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;do&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    dmDados&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;queCidade&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;ParamByName(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;ID&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Value&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; i&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    dmDados&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;queCidade&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;ParamByName(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;NOME&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Value&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;SANTAREM&#39;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    dmDados&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;queCidade&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;ExecSql&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  dmDados&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;dbBanco&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Commit&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  b &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;time&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  ShowMessage(&lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;IntToStr&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(MilliSecondsBetween(b&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;a)))&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;br /&gt;
Neste caso o tempo médio de execução do código foi de 2,9 segundos. Um ganho de tempo considerável em relação à execução em modo padrão.&lt;br /&gt;
Fizemos também um teste usando &lt;b&gt;TZUpdateSQL&lt;/b&gt;, implementado de acordo com o post citado acima. O código é apresentado em seguida e o tempo médio de execução foi de 3,8 segundos. Um desempenho um pouco pior do que o teste anterior.&lt;br /&gt;
&lt;br /&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;var&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  i&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd;&quot;&gt;integer&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  a&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; b&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd;&quot;&gt;TDateTime&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  a &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;time&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  dmDados&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;queCidade&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Open&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  dmDados&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;dbBanco&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;StartTransaction&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;to&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ffb86c;&quot;&gt;5000&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;do&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;begin&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    dmDados&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;queCidade&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Insert&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    dmDados&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;queCidade&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;FieldByName(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;ID&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Value &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; i&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    dmDados&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;queCidade&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;FieldByName(&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;NOME&#39;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Value&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;SANTAREM&#39;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;    dmDados&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;queCidade&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Post&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  dmDados&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;dbBanco&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Commit&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  b &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;time&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;  ShowMessage(&lt;/span&gt;&lt;span style=&quot;color: #8be9fd; font-style: italic;&quot;&gt;IntToStr&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;(MilliSecondsBetween(b&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;a)))&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #ff79c6;&quot;&gt;end;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;br /&gt;
Estes teste foram feitos em Windows e servidor de BD e cliente rodando na mesma máquina. Certamente se você executar esses testes em outro ambiente, os tempos serão diferentes, mas comparativamente você deverá chegar à mesma conclusão.&lt;br /&gt;
Sabe-se que uma falha no sistema, enquanto uma transação estiver ativa, causa a execução de um ROLLBACK pelo SGBD, fazendo com que todas as atualizações feitas a partir do BEGIN sejam canceladas. Portanto, use esta recomendação com cautela e bom senso. Longas transações ativas estão sujeitas a perdas de dados muito maiores que transações curtas, em caso de falhas no sistema.&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;color: white;&quot;&gt;&lt;span style=&quot;background-color: red;&quot;&gt;Atualizado em 28/09/2010.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://professorcarlos.blogspot.com/feeds/4593694644821961880/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/33774862/4593694644821961880?isPopup=true' title='13 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/4593694644821961880'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/4593694644821961880'/><link rel='alternate' type='text/html' href='http://professorcarlos.blogspot.com/2010/09/lazarus-autocommit-zeos-pg.html' title='Lazarus - AutoCommit no ZeosLib com PostgreSQL'/><author><name>Professor Carlos</name><uri>http://www.blogger.com/profile/10347668022241306511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFBZD8558AE67R-CINkytW3INbUINaAhDuZ7GKm_4DSSKeHSpdm3K-TPQrhNPn5B-21wN24ZH8flimqC7ikTzmRrYZBAsyD_JSC8YW8BDJZwmTby9R7LZC9SYkyHkSs7NPVZxXiqEDqjhkRJNxZwqqyZaVgYjISiFpzXsSyGZmL1YFCQ/s220/WhatsApp%20Image%202024-05-17%20at%2017.42.05.jpeg'/></author><thr:total>13</thr:total></entry><entry><id>tag:blogger.com,1999:blog-33774862.post-6825120760390071162</id><published>2010-08-18T16:03:00.006-03:00</published><updated>2025-11-27T18:35:08.930-03:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="componentes lazarus"/><category scheme="http://www.blogger.com/atom/ns#" term="Lazarus"/><category scheme="http://www.blogger.com/atom/ns#" term="PDA"/><category scheme="http://www.blogger.com/atom/ns#" term="WinCE"/><title type='text'>Lazarus - Aplicação WinCE usando tabelas DBF</title><content type='html'>&lt;h1 style=&quot;text-align: left;&quot;&gt;Lazarus - Aplicação WinCE usando tabelas DBF&lt;/h1&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;Há algum tempo eu escrevi um artigo aqui no blog chamado &lt;a href=&quot;https://professorcarlos.blogspot.com/2010/03/lazarus-criando-uma-aplicacao-para.html&quot;&gt;Lazarus - Criando uma aplicação para WinCE&lt;/a&gt;. Nesse artigo usei SQLite para WinCE. E a implementação tinha bastante código pois usava a unit &lt;b&gt;sqlite3ds&lt;/b&gt;. Agora vou mostrar que é possível desenvolver usando tabelas Dbase, aquelas que tem a extensão DBF. Para isso vamos usar o componente &lt;b&gt;TDbf&lt;/b&gt; da aba &lt;b&gt;Data Access&lt;/b&gt;. A seguir os passos necessários:&lt;br /&gt;
&lt;br /&gt;
1) Crie uma tabela Dbase.&lt;br /&gt;
2) Inicie uma nova aplicação e coloque um &lt;b&gt;TDbf&lt;/b&gt; no &lt;b&gt;DataModule&lt;/b&gt;. Na propriedade &lt;b&gt;TableName&lt;/b&gt; localize a tabela que foi criada.&lt;br /&gt;
3) Nas propriedades &lt;b&gt;FilePath&lt;/b&gt; e &lt;b&gt;FilePathFull&lt;/b&gt; ele vai automaticamente definir o caminho do PC onde está a tabela. Depois vamos resolver isso.&lt;br /&gt;
4) Coloque um &lt;b&gt;TDataSource&lt;/b&gt; no &lt;b&gt;Data Module&lt;/b&gt; e faça a associação necessária com o &lt;b&gt;TDbf&lt;/b&gt;.&lt;br /&gt;
5) Agora vamos ao form da interface e coloque os controles (da aba &lt;b&gt;Data Controls&lt;/b&gt;) necessários para cada campo da tabela. Associe a propriedade &lt;b&gt;DataSource&lt;/b&gt; de cada controle com o &lt;b&gt;DataSource &lt;/b&gt;inserido no passo 4. Na propriedade &lt;b&gt;DataField&lt;/b&gt; você pode digitar o nome do campo da tabela DBF ou, se a tabela estiver ativa, selecionar na caixa combinada. Lembre que neste form deve ser incluida a linha &lt;b&gt;uses&lt;/b&gt; referente a &lt;i&gt;unit&lt;/i&gt; do &lt;b&gt;Data Module&lt;/b&gt;.&lt;br /&gt;
6) Coloque um &lt;b&gt;TDBNavigator&lt;/b&gt; e ligue a propriedade &lt;b&gt;DataSource&lt;/b&gt; adequadamente.&lt;br /&gt;
7) No evento &lt;b&gt;OnShow&lt;/b&gt; do form digite o seguinte:&lt;br /&gt;
&lt;br /&gt;
&lt;!--HTML generated using hilite.me--&gt;&lt;div style=&quot;background: rgb(40, 42, 54); border-color: gray; border-image: initial; border-style: solid; border-width: 0.1em 0.1em 0.1em 0.8em; border: solid gray; overflow: auto; padding: 0.2em 0.6em; width: auto;&quot;&gt;&lt;pre style=&quot;line-height: 125%; margin: 0px;&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;dbf1&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;FilePath &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;dbf1&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;FilePathFull &lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;:=&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #bd93f9;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;span style=&quot;color: #f8f8f2;&quot;&gt;dbf1&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #f8f8f2;&quot;&gt;Open&lt;/span&gt;&lt;span style=&quot;color: #ff79c6;&quot;&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;br /&gt;
Se você tiver mudado a propriedade &lt;b&gt;Name&lt;/b&gt; do &lt;b&gt;TDbf&lt;/b&gt;, substitua o &lt;i&gt;dbf1&lt;/i&gt; no código acima pelo nome que você escolheu. &lt;br /&gt;
Para compilar um projeto WinCE é necessário fazer algumas  configurações em &lt;b&gt;Project -&amp;gt; Project options&lt;/b&gt;. Na opção  &lt;b&gt;Paths&lt;/b&gt; abaixo de &lt;b&gt;Compiler Options&lt;/b&gt; selecione &lt;i&gt;wince&lt;/i&gt; em &lt;b&gt;LCL Widget Type  (various)&lt;/b&gt;. Na opção &lt;b&gt;Code generation&lt;/b&gt; escolha &lt;i&gt;WinCE&lt;/i&gt; em  &lt;b&gt;Target OS (-T)&lt;/b&gt; e &lt;i&gt;arm&lt;/i&gt; em &lt;b&gt;Target CPU family  (-P)&lt;/b&gt;. Pronto. O compilador irá gerar um executável que executa apenas  no PDA. Dessa forma para compilar use sempre &lt;b&gt;CTRL + F9&lt;/b&gt;. Nos testes eu usei o &lt;b&gt;Lazarus 0.9.29&lt;/b&gt;. Se você utiliza a versão &lt;b&gt;0.9.28&lt;/b&gt; o caminho para essas configurações está em &lt;b&gt;Project -&amp;gt; Compiler options&lt;/b&gt;.&lt;br /&gt;
Copie o executável e a tabela para o PDA e execute. Pronto, simples assim. Fiz testes com sucesso em um PDA com WM 5 e no emulador WM 6.</content><link rel='replies' type='application/atom+xml' href='http://professorcarlos.blogspot.com/feeds/6825120760390071162/comments/default' title='Postar comentários'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/33774862/6825120760390071162?isPopup=true' title='14 Comentários'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/6825120760390071162'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/33774862/posts/default/6825120760390071162'/><link rel='alternate' type='text/html' href='http://professorcarlos.blogspot.com/2010/08/lazarus-wince-com-dbf.html' title='Lazarus - Aplicação WinCE usando tabelas DBF'/><author><name>Professor Carlos</name><uri>http://www.blogger.com/profile/10347668022241306511</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiFBZD8558AE67R-CINkytW3INbUINaAhDuZ7GKm_4DSSKeHSpdm3K-TPQrhNPn5B-21wN24ZH8flimqC7ikTzmRrYZBAsyD_JSC8YW8BDJZwmTby9R7LZC9SYkyHkSs7NPVZxXiqEDqjhkRJNxZwqqyZaVgYjISiFpzXsSyGZmL1YFCQ/s220/WhatsApp%20Image%202024-05-17%20at%2017.42.05.jpeg'/></author><thr:total>14</thr:total></entry></feed>