Apps Script Coding Examples FREE PDF Guide Download Now

Bullet code copy

example of how you can copy a bulleted list from one Google Document to another using Google Apps Script:

// Function to copy the bulleted list from one document to another

function copyBulletedList() {

  // Get the source document

  const sourceDoc = DocumentApp.getActiveDocument();

  // Get the list items from the source document

  const sourceListItems = sourceDoc.getBody().getListItems();

  // Create a new Google Document

  const targetDoc = DocumentApp.create(“Target Document”);

  // Get the body of the target document

  const targetBody = targetDoc.getBody();

  // Loop through each source list item

  for (const i = 0; i < sourceListItems.length; i++) {

    // Get the text of the current list item

    const listItemText = sourceListItems[i].getText();

    // Add the text as a bulleted list item in the target document

    targetBody.appendListItem(listItemText).setGlyphType(DocumentApp.GlyphType.BULLET);

  }

}

Note: This script assumes that the source document has at least one bulleted list item. If the source document doesn’t have any bulleted list items, the script will throw an error.

function copyBullets() {

  //const sourceDoc = DocumentApp.getActiveDocument();

  const sourceDoc = DocumentApp.openById(‘1SCE46vJBBIYWKFjiwZCXXVY’);

  const listItems = sourceDoc.getBody().getListItems();

  Logger.log(listItems);

  //const targetDoc = DocumentApp.create(‘New Doc Bullets’);

  const targetDoc = DocumentApp.openById(‘1KCwguT8iHINHXlH-I4’);

  const targetBody = targetDoc.getBody();

  for(let i=0;i<listItems.length;i++){

    const listItemText = listItems[i].getText();

    targetBody.appendListItem(listItemText).setGlyphType(DocumentApp.GlyphType.BULLET);

  }

}

Sending an email

Sending an email using Google Apps Script:

function sendEmails() {

  // Get the active Gmail account

  const email = Session.getActiveUser().getEmail();

  // Define the recipient and subject of the email

  const recipient = “recipient@example.com”;

  const subject = “Test Email”;

  // Define the body of the email

  const body = “This is a test email sent from Google Apps Script”;

  // Send the email

  GmailApp.sendEmail(recipient, subject, body, {from: email});

}

In this example, the script uses the GmailApp class to send an email. The sendEmails() function first retrieves the email address of the active Gmail account using the Session.getActiveUser().getEmail() method. The recipient, subject, and body of the email are then defined as variables. Finally, the GmailApp.sendEmail() method is used to send the email, passing in the recipient, subject, body, and sender email address as arguments.

function sendMyEmail() {

  const email = Session.getActiveUser().getEmail();

  const recEmail = email;

  const subject = ‘Test Emailing’;

  const body = ‘<h1>Laurence Svekis</h1><p>This is an email sent by script</p>’;

  const id = ‘1SCE46vJiwZCXXVY’;

  const file = DriveApp.getFileById(id);

  const blob = Utilities.newBlob(‘My Content’,’text/html’,’myHTML.html’);

  /*

  GmailApp.sendEmail(recEmail,subject,body,{

    from:email

  });

  */

  MailApp.sendEmail({

    to:recEmail,

    subject:subject,

    name:’My Docs’,

    htmlBody:body,

    attachments:[file.getAs(MimeType.PDF)]

  });

}

Reading data from a Google Sheet

Reading data from a Google Sheet:

function readData() {

  // Get the active sheet

  const sheet = SpreadsheetApp.getActiveSheet();

  // Get the data range from the sheet

  const dataRange = sheet.getDataRange();

  // Get the values from the data range

  const data = dataRange.getValues();

  // Log the data to the console

  Logger.log(data);

}

In this example, the readData() function uses the SpreadsheetApp class to read data from a Google Sheet. The SpreadsheetApp.getActiveSheet() method is used to get the active sheet, and the sheet.getDataRange() method is used to get the data range from the sheet. The dataRange.getValues() method is then used to get the values from the data range. Finally, the values are logged to the console using the Logger.log() method.

function readData(){

  const sheet = SpreadsheetApp.getActiveSheet();

  const dataRange = sheet.getDataRange();

  const data = dataRange.getValues();

  data.forEach(row=>{

    Logger.log(row.length);

  })

  Logger.log(data);

}

Writing data to a Google Sheet

Writing data to a Google Sheet:

function writeData() {

  // Get the active sheet

  const sheet = SpreadsheetApp.getActiveSheet();

  // Define the data to be written to the sheet

  const data = [[“Name”, “Age”], [“John”, 30], [“Jane”, 28]];

  // Get the last row in the sheet

  const lastRow = sheet.getLastRow();

  // Write the data to the sheet starting from the next row after the last row

  sheet.getRange(lastRow + 1, 1, data.length, data[0].length).setValues(data);

}

In this example, the writeData() function uses the SpreadsheetApp class to write data to a Google Sheet. The SpreadsheetApp.getActiveSheet() method is used to get the active sheet, and the data to be written to the sheet is defined as a two-dimensional array. The sheet.getLastRow() method is used to get the last row in the sheet, and the sheet.getRange() method is used to get a range starting from the next row after the last row. The setValues() method is then Writing data to a Google Sheet:

  // Write the data to the sheet starting from the next row after the last row

  sheet.getRange(lastRow + 1, 1, data.length, data[0].length).setValues(data);

}

The setValues() method is then used to write the data to the sheet, passing in the data as an argument. The first argument to getRange() specifies the starting row (lastRow + 1), the second argument specifies the starting column (1), the third argument specifies the number of rows (data.length), and the fourth argument specifies the number of columns (data[0].length).

function writeData(){

  const ss = SpreadsheetApp.getActiveSpreadsheet();

  const sheets = ss.getSheets();

  Logger.log(sheets);

  const sheet = sheets[2];

  //const sheet = ss.getSheetByName(‘Sheet2’);

  //const sheet = SpreadsheetApp.getActiveSheet();

  const data = [[‘Name’,’ID’],[‘Laurence’,’100′],[‘Laura’,’50’],[‘John’,’1140′]];

  const lastRow = sheet.getLastRow();

  Logger.log(lastRow);

  const range = sheet.getRange(lastRow+1,1,data.length,data[0].length);

  range.setValues(data);

}

Generating a PDF from a Google Document

Generating a PDF from a Google Document:

function generatePdf() {

  // Get the active document

  const document = DocumentApp.getActiveDocument();

  // Get the URL of the active document

  const url = document.getUrl();

  // Define the file name for the PDF

  const fileName = “Test PDF”;

  // Generate the PDF

  const pdf = UrlFetchApp.fetch(url + “&exportFormat=pdf”).getBlob();

  // Save the PDF to Drive

  DriveApp.createFile(pdf).setName(fileName);

}

In this example, the generatePdf() function uses the DocumentApp and UrlFetchApp classes to generate a PDF from a Google Document. The DocumentApp.getActiveDocument() method is used to get the active document, and the document.getUrl() method is used to get the URL of the document. The file name for the PDF is defined as a variable, and the UrlFetchApp.fetch() method is used to fetch the URL of the document with the &exportFormat=pdf parameter appended to it, which causes the document to be exported as a PDF. The getBlob() method is used to get the binary data of the PDF, and the DriveApp.createFile() method is used to save the PDF to Google Drive.

function generatePdf() {

  const url = ‘https://basescripts.com/wp-content/uploads/2023/02/JavaScript-interview-questions-with-code-examples.pdf’;

  const fileName = “Test PDF”;

  const pdf = UrlFetchApp.fetch(url).getBlob();

  DriveApp.createFile(pdf).setName(fileName);

}

Get a web PDF and save it to your gDrive

const ID = ‘1SCE46vJBcW0LBIYWKFjiwZCXXVY’;

const FOLDERID = ‘1sFNkLwdPK6ggAUlWfunctiowZ’;

function genPDF(){

  const doc = DriveApp.getFileById(ID);

  const pdf = doc.getAs(‘application/pdf’);

  pdf.setName(‘test1’);

  const folder = DriveApp.getFolderById(FOLDERID);

  folder.createFile(pdf);

}

Converting a Google Document to plain text

Converting a Google Document to plain text:

function convertToPlainText() {

  // Get the active document

  const document = DocumentApp.getActiveDocument();

  // Get the body of the document

  const body = document.getBody();

  // Get the plain text representation of the document

  const plainText = body.getText();

  // Log the plain text to the console

  Logger.log(plainText);

}

In this example, the convertToPlainText() function uses the DocumentApp class to convert a Google Document to plain text. The DocumentApp.getActiveDocument() method is used to get the active document, and the document.getBody() method is used to get the body of the document. The body.getText() method is then used to get the plain text representation of the document, which is logged to the console using the Logger.log() method.