No title

 


function sendSelectedArticleToBloggerDraft() {

  if (BLOGGER_CONFIG.AUTO_PUBLISH_ENABLED !== false) {

    throw new Error(

      "This draft test requires AUTO_PUBLISH_ENABLED to be false."

    );

  }


  const ss = SpreadsheetApp.openById(

    "1YU7DpmtpsDXi_r5gTscTXKsRCy1mc-QYYYPpjczfxIE"

  );


  const sheet = ss.getSheetByName("AI Drafts");

  if (!sheet) throw new Error("AI Drafts sheet not found.");


  const activeSheet = ss.getActiveSheet();

  const row = activeSheet.getActiveRange().getRow();


  if (activeSheet.getName() !== "AI Drafts" || row < 2) {

    throw new Error(

      "Select an article row (row 2 or below) in AI Drafts first."

    );

  }


  const values = sheet.getRange(row, 1, 1, 10).getValues()[0];

  const title = String(values[4] || "").trim();

  const article = String(values[5] || "").trim();

  const sources = String(values[3] || "").trim();

  const existingPostId = String(values[8] || "").trim();


  if (!title || !article) {

    throw new Error("The selected row needs a title and article draft.");

  }


  if (existingPostId) {

    throw new Error(

      "This row already has a Blogger Post ID. No new post was created."

    );

  }


  const content =

    "<p><strong>Sources:</strong> " +

    sources.split(/\s+/)

      .filter(function(link) {

        return /^https?:\/\/\S+$/i.test(link);

      })

      .map(function(link) {

        return '<a href="' + link.replace(/&/g, "&amp;")

          .replace(/"/g, "&quot;") + '">' +

          link + '</a>';

      }).join(" | ") +

    "</p><hr>" + article;


  const endpoint =

    "https://www.googleapis.com/blogger/v3/blogs/" +

    BLOGGER_CONFIG.BLOG_ID +

    "/posts/?isDraft=true";


  const response = UrlFetchApp.fetch(endpoint, {

    method: "post",

    contentType: "application/json",

    headers: {

      Authorization: "Bearer " + ScriptApp.getOAuthToken()

    },

    payload: JSON.stringify({

      title: title,

      content: content

    }),

    muteHttpExceptions: true

  });


  const status = response.getResponseCode();

  const body = response.getContentText();

  Logger.log("Blogger HTTP status: " + status);

  Logger.log(body);


  if (status !== 200 && status !== 201) {

    throw new Error("Blogger rejected the request. Check the log.");

  }


  const post = JSON.parse(body);


  if (post.status !== "DRAFT") {

    throw new Error(

      "STOP: Blogger returned status " + post.status +

      ". Do not continue. Check Blogger Posts immediately."

    );

  }


  sheet.getRange(row, 9).setValue(String(post.id));

  sheet.getRange(row, 10).setValue("AWAITING APPROVAL");

  sheet.getRange(row, 8).setValue("BLOGGER DRAFT - REVIEW");


  Logger.log("SUCCESS: Blogger draft created with ID " + post.id);

}


Post a Comment

Previous Post Next Post

Homepage In-Feed Ad Slot

Sidebar Ad Slot