JSFiddle - React, Tailwind, and code Playground

by schantanu

JavaScript

function onOpen() {
  DocumentApp.getUi()
    .createMenu('Custom Tools')
    .addItem('Add Project', 'addProject')
    .addItem('Archive Project', 'archiveProject')
    .addToUi();
}

function addProject() {
  const ui = DocumentApp.getUi();
  
  // Prompt for project name only
  const projectNameResponse = ui.prompt(
    'Project Name',
    'Enter the project name:',
    ui.ButtonSet.OK_CANCEL
  );
  
  if (projectNameResponse.getSelectedButton() == ui.Button.CANCEL) return;
  const projectName = projectNameResponse.getResponseText();
  
  // Use placeholder values for the rest
  const requestorName = "<Requestor Name>";
  const projectOverview = "<Project Overview>";
  const tasks = ["Task 1", "Task 2", "Task 3"];
  
  // Get the document and body
  const doc = DocumentApp.getActiveDocument();
  const body = doc.getBody();
  
  // Create and append project heading as Heading 3
  const projectHeading = body.appendParagraph(projectName);
  projectHeading.setHeading(DocumentApp.ParagraphHeading.HEADING3);
  
  // Add the rest of the content as normal paragraphs
  const requestorLine = body.appendParagraph("Requestor: " + requestorName);
  const overviewLine = body.appendParagraph("Overview: " + projectOverview);
  const tasksHeader = body.appendParagraph("Tasks:");
  
  // Add tasks as bullet points
  tasks.forEach(task => {
    const taskItem = body.appendListItem(task);
    taskItem.setGlyphType(DocumentApp.GlyphType.BULLET);
  });
  
  ui.alert('Success', 'Project has been added to the document!', ui.ButtonSet.OK);
}

function archiveProject() {
  const ui = DocumentApp.getUi();
  const doc = DocumentApp.getActiveDocument();
  const body = doc.getBody();
  
  // Find all Heading 3 paragraphs (project names)
  const projectNames = [];
  const headingElements = [];
  
  const paragraphs = body.getParagraphs();
  for (let i = 0; i < paragraphs.length; i++) {
    if (paragraphs[i].getHeading() === DocumentApp.ParagraphHeading.HEADING3) {
     ...