if cell contains 0 then skip row

78 views
Skip to first unread message

Celeste Mae Bolhano

unread,
Mar 19, 2023, 6:51:49 PM3/19/23
to Google Apps Script Community
Hello, this is my code how

function onOpen() {
  const ui = SpreadsheetApp.getUi();
  const menu = ui.createMenu('AutoFill Docs');
  menu.addItem('Create New Docs''createNewGoogleDocs')
  menu.addToUi();
}


function createNewGoogleDocs() {
  //This value should be the id of your document template that we created in the last step
  const googleDocTemplate = DriveApp.getFileById('1fP4HxVcgZSfMJKpk6FVY0-FvZQk7VN3cloKjm-AHT7M');
  
  //This value should be the id of the folder where you want your completed documents stored
  const destinationFolder = DriveApp.getFolderById('1i9iMftLb1ns4UZ1pt8R_WWXmnW9XHwe1')
  //Here we store the sheet as a variable
  const sheet = SpreadsheetApp
    .getActiveSpreadsheet()
    .getSheetByName('MAIN');
  
  //Now we get all of the values as a 2D array
  const rows = sheet.getDataRange().getValues();


  
  //Start processing each spreadsheet row
  rows.forEach(function(rowindex){
    //Here we check if this row is the headers, if so we skip it
    if (index === 0return;
    //Here we check if a document has already been generated by looking at 'Document Link', if so we skip it
    if (row[28]) return;
    //Using the row data in a template literal, we make a copy of our template document in our destinationFolder
    const copy = googleDocTemplate.makeCopy(`${row[0]} - ${row[1]} - Order Printout` , destinationFolder)
    //Once we have the copy, we then open it using the DocumentApp
    const doc = DocumentApp.openById(copy.getId())
    //All of the content lives in the body, so we get that for editing
    const body = doc.getBody();
    //In this line we do some friendly date formatting, that may or may not work for you locale
    const friendlyDate = new Date(row[3]).toLocaleDateString();
    
    //In these lines, we replace our replacement tokens with values from our spreadsheet row
    body.replaceText('{{Order #}}'row[0]);
    body.replaceText('{{Customer Name}}'row[1]);
    body.replaceText('{{Customer Phone}}'row[2]);
    body.replaceText('{{Receiver Name}}'row[3]);
    body.replaceText('{{Receiver Phone}}'row[4]);
    body.replaceText('{{Address}}'row[5]);
    body.replaceText('{{Orchid - Dendronium - 1}}'row[6]);
    body.replaceText('{{OD - Price - 1}}'row[7]);
    body.replaceText('{{Orchid - Dendronium - 2}}'row[8]);
    body.replaceText('{{OD - Price - 2}}'row[9]);
    body.replaceText('{{Orchid - Vanda - 1}}'row[10]);
    body.replaceText('{{OV - Price - 1}}'row[11]);
    body.replaceText('{{Orchid - Vanda - 2}}'row[12]);
    body.replaceText('{{OV - Price - 2}}'row[13]);
    body.replaceText('{{Bouquet 1}}'row[14]);
    body.replaceText('{{B - Price - 1}}'row[15]);
    body.replaceText('{{Bouquet 2}}'row[16]);
    body.replaceText('{{B - Price - 2}}'row[17]);
    body.replaceText('{{Delivery Fee}}'row[18]);
    body.replaceText('{{Total Price}}'row[19]);
    body.replaceText('{{Payment Method}}'row[20]);
    body.replaceText('{{Delivery Options}}'row[21]);
    body.replaceText('{{Delivery Time}}'row[22]);
    body.replaceText('{{Payment Status}}'row[24]);
    body.replaceText('{{Notes}}'row[27]);
    
    //We make our changes permanent by saving and closing the document
    doc.saveAndClose();
    //Store the url of our new document in a variable
    const url = doc.getUrl();
    //Write that value back to the 'Document Link' column in the spreadsheet. 
    sheet.getRange(index + 129).setValue(url)
    
  })
  
}

If the following replace text are 0, then skip then and it should not show up in the google doc:
{{Orchid - Dendronium - 1}} {{OD - Price - 1}}

What code should I be using for this?

cbmserv...@gmail.com

unread,
Mar 19, 2023, 7:23:12 PM3/19/23
to google-apps-sc...@googlegroups.com

Hi Celeste,

 

I am not sure what you mean by if 0 then skip row. It would depend on what your doc template looks like to make this.

 

From your script perspective alone, if you don’t want to do a replacetext when value of that cell item is zero, do the following:

 

    if (row[6] !=0)

{body.replaceText('{{Orchid - Dendronium - 1}}'row[6]);

      body.replaceText('{{OD - Price - 1}}'row[7]); }

 

    if (row[8] != 0 )

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/910e7f88-5fcc-4f9a-801a-0461932aee77n%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages