Home > Software design >  How do you loop through an object and replace text
How do you loop through an object and replace text

Time:10-13

I have a script that should create a pdf file from a google form submission and grabs the data to be changed as an object. However I am using the replaceText action to make the changes to the doc and I'm getting the following error.

Exception: Invalid argument: replacement at Create_PDF(Code:37:8) at After_Submit(Code:13:19)

It is supposed to change the values in the generated doc file and it worked when I used the namedValues function. However now that I'm using range instead it doesn't seem to work.

function After_Submit(e){
  var range = e.range;
  var row = range.getRow(); //get the row of newly added form data
  var sheet = range.getSheet(); //get the Sheet
  var headers = sheet.getRange(1, 1, 1, 129).getValues().flat(); //get the header names from A-O
  var data = sheet.getRange(row, 1, 1, headers.length).getValues(); //get the values of newly added form data   formulated values
  var values = {}; // create an object 
  for( var i = 0; i < headers.length; i   ){
    values[headers[i]] = data[0][i]; //add elements to values object and use headers as key
  }

  Logger.log(values);
  const pdfFile = Create_PDF(values);
  sendEmail(e.namedValues['Email Address to Receive File '][0],pdfFile);
}

function sendEmail(email,pdfFile){
  
  GmailApp.sendEmail(email, "Subject", "Files Attached", {
    attachments: [pdfFile], 
    name: "From Email"

  });
 
}
function Create_PDF(values) {
  const PDF_folder = DriveApp.getFolderById("ID_1");
  const TEMP_FOLDER = DriveApp.getFolderById("ID_2");
  const PDF_Template = DriveApp.getFileById('ID_3');
  
  const newTempFile = PDF_Template.makeCopy(TEMP_FOLDER);
  const  OpenDoc = DocumentApp.openById(newTempFile.getId());
  const body = OpenDoc.getBody();
  
  console.log(body);

  body.replaceText("{{Timestamp}}", values['Timestamp'][0]);
  body.replaceText("{{Location}}", values['Location'][0]);
  body.replaceText("{{Item1}}", values['Item1'][0]);
  body.replaceText("{{Item2}}", values['Item2'][0]);
  body.replaceText("{{Itme3}}", values['Item3'][0]);
  body.replaceText("{{e1}}", values['e1'][0]);
  body.replaceText("{{e2}}", values['e2'][0]);
  body.replaceText("{{e3}}", values['e3'][0]);
  body.replaceText("{{e4}}", values['e4'][0]);
  body.replaceText("{{e5}}", values['e5'][0]);
  body.replaceText("{{e6}}", values['e6'][0]);
  body.replaceText("{{e7}}", values['e7'][0]);
  body.replaceText("{{e8}}", values['e8'][0]);
  body.replaceText("{{e9}}", values['e9'][0]);
  body.replaceText("{{e10}}", values['e10'][0]);
  body.replaceText("{{e11}}", values['e11'][0]);
  body.replaceText("{{e12}}", values['e12'][0]);
  body.replaceText("{{e13}}", values['e13'][0]);
  body.replaceText("{{e14}}", values['e14'][0]);
  body.replaceText("{{e15}}", values['e15'][0]);
  body.replaceText("{{e16}}", values['e16'][0]);
  body.replaceText("{{e17}}", values['e17'][0]);
  body.replaceText("{{e18}}", values['e18'][0]);
  body.replaceText("{{e19}}", values['e19'][0]);
  body.replaceText("{{e20}}", values['e20'][0]);
  body.replaceText("{{e21}}", values['e21'][0]);
  body.replaceText("{{e22}}", values['e22'][0]);
  body.replaceText("{{e23}}", values['e23'][0]);
  body.replaceText("{{e24}}", values['e24'][0]);
  body.replaceText("{{e25}}", values['e25'][0]);
  body.replaceText("{{e26}}", values['e26'][0]);
  body.replaceText("{{e27}}", values['e27'][0]);
  body.replaceText("{{e28}}", values['e28'][0]);
  body.replaceText("{{e29}}", values['e29'][0]);
  body.replaceText("{{e30}}", values['e30'][0]);
  body.replaceText("{{e31}}", values['e31'][0]);
  body.replaceText("{{e32}}", values['e32'][0]);
  body.replaceText("{{e33}}", values['e33'][0]);
  body.replaceText("{{e34}}", values['e34'][0]);
  body.replaceText("{{e35}}", values['e35'][0]);
  body.replaceText("{{e36}}", values['e36'][0]);
  body.replaceText("{{e37}}", values['e37'][0]);
  body.replaceText("{{e38}}", values['e38'][0]);
  body.replaceText("{{e39}}", values['e39'][0]);
  body.replaceText("{{H1}}", values['H1'][0]);
  body.replaceText("{{H2}}", values['H2'][0]);
  body.replaceText("{{H3}}", values['H3'][0]);
  body.replaceText("{{H4}}", values['H4'][0]);
  body.replaceText("{{H5}}", values['H5'][0]);
  body.replaceText("{{H6}}", values['H6'][0]);
  body.replaceText("{{H7}}", values['H7'][0]);
  body.replaceText("{{H8}}", values['H8'][0]);
  body.replaceText("{{H9}}", values['H9'][0]);
  body.replaceText("{{H10}}", values['H10'][0]);
  body.replaceText("{{H11}}", values['H11'][0]);
  body.replaceText("{{H12}}", values['H12'][0]);
  body.replaceText("{{H13}}", values['H13'][0]);
  body.replaceText("{{H14}}", values['H14'][0]);


  OpenDoc.saveAndClose();
  

  const BLOBPDF = newTempFile.getAs(MimeType.PDF);
  const pdfFile =  PDF_folder.createFile(BLOBPDF).setName("FLHA");
  console.log("The file has been created ");
  
  return pdfFile;

}

CodePudding user response:

Your question was how to loop through an object and replace text

This creates an object from Sheet0:

Sheet0:

one pattern
two this is the pattern
three pattern pattern
four nothing
five nothing

Code:

function replacepattern() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Sheet0');
  const vs = sh.getRange(1,1,sh.getLastRow(), 2).getValues();
  //creating object from spreadsheet
  let obj = {pA:[]};
  vs.forEach(r =>{
    obj[r[0]]=r[1];
    obj.pA.push(r[0]);
  });
  Logger.log(JSON.stringify(obj));
  let oA = obj.pA.map(p => [obj[p].replace(/pattern/g,'replacement')]);//doing the replacement in an object
  sh.getRange(1,sh.getLastColumn()   1,oA.length, oA[0].length).setValues(oA);//outputting the replaced string in the next column
  Logger.log(JSON.stringify(oA));
}

Sheet0 after running once:

one pattern replacement
two this is the pattern this is the replacement
three pattern pattern replacement replacement
four nothing nothing
five nothing nothing

CodePudding user response:

This is related to my answer enter image description here

Form inputs:

enter image description here

Output:

enter image description here

Reference:

  • Related