Meu script está correto no Google Ads?

12 views
Skip to first unread message

Bárbara Brugger

unread,
Sep 8, 2025, 12:35:50 AMSep 8
to Google Ads Scripts Forum
Quero saber se meu script está correto. Quero que o investimento de todas as campanhas seja reduzido no sábado às 00:05 e retorne ao valor original na segunda feira às 00:05

function main() {
  var spreadsheetUrl = "https://docs.google.com/spreadsheets/d/1Vf1rzu6gQ8RZy0suEaXylFCCP5t6uXuJzATAGZIPfBg/edit?usp=sharing";
  var sheetName = "Orcamentos";
  var ss = SpreadsheetApp.openByUrl(spreadsheetUrl);
  var sheet = ss.getSheetByName(sheetName);
 
  var campaigns = AdsApp.campaigns()
    .withCondition("Status = ENABLED")
    .get();
 
  var hoje = new Date();
  var diaSemana = hoje.getDay(); // 0=domingo, 1=segunda, ..., 6=sábado
 
  while (campaigns.hasNext()) {
    var campanha = campaigns.next();
    var nome = campanha.getName();
    var linha = procurarLinha(sheet, nome);
   
    if (!linha) {
      // Se a campanha não existir na planilha, cria
      linha = sheet.appendRow([nome, campanha.getBudget().getAmount(), ""]);
      continue;
    }
   
    var valorOriginal = sheet.getRange(linha, 2).getValue();
    var orcamentoAtual = campanha.getBudget().getAmount();
   
    if (diaSemana == 6) { // sábado 00:05 → reduzir em 40%
      var novoValor = valorOriginal * 0.6;
      campanha.getBudget().setAmount(novoValor);
      Logger.log("Campanha " + nome + " reduzida para " + novoValor);
    }
   
    if (diaSemana == 1) { // segunda 00:05 → restaurar e atualizar o valor base
      campanha.getBudget().setAmount(valorOriginal);
      sheet.getRange(linha, 2).setValue(valorOriginal); // garante base
      Logger.log("Campanha " + nome + " restaurada para " + valorOriginal);
    }
   
    if (diaSemana >= 2 && diaSemana <= 5) {
      // terça a sexta → atualizar valor base se foi alterado manualmente
      if (orcamentoAtual != valorOriginal) {
        sheet.getRange(linha, 2).setValue(orcamentoAtual);
        Logger.log("Valor base atualizado para campanha " + nome + ": " + orcamentoAtual);
      }
    }
  }
}

function procurarLinha(sheet, nomeCampanha) {
  var data = sheet.getRange(1, 1, sheet.getLastRow(), 1).getValues();
  for (var i = 0; i < data.length; i++) {
    if (data[i][0] == nomeCampanha) {
      return i + 1;
    }
  }
  return null;
}

Google Ads Scripts Forum Advisor

unread,
Sep 8, 2025, 2:09:50 AMSep 8
to adwords...@googlegroups.com

Hi,

Thank you for reaching out to the Google Ads Scripts support team.

Our team can support your case better in English, but we can continue to use our translation tools to support you in the current language if you prefer. You can simply reply back in English if you’d like to switch.

Kindly note that our team will not be able to review complete scripts for correctness. If you are running into a specific issue with the environment or with the AdsApp functionality, we would be able to assist you.

In order to investigate further on the issue, do reach out to us with the following details.

  • Google Ads account ID/CID
  • Name of the affected script
  • Screenshot of the issue (if any)
  • Shareable spreadsheet link if you are using any in the script. You may refer to this article to share a file publicly

You can share the requested details via Reply privately to the author option or a direct private reply to this email.

Thanks,
 
Google Logo Google Ads Scripts Team

Feedback
How was our support today?

rating1    rating2    rating3    rating4    rating5
[2025-09-08 06:09:10Z GMT] This message is in relation to case "ref:!00D1U01174p.!500Ht01u62zw:ref" (ADR-00332114)



Reply all
Reply to author
Forward
0 new messages