Replace text in header script not working - google-apps-script
I wrote a script using Google Apps Script to allow data from Google Sheets (via Google Form input) to populate a Google Document. It works fine except it will not work to replace text in the header. Replacing text in the body works just fine and the document populates just fine without errors. I am looking to put the same exact text in each header so it should not be complicated. I use this same script for multiple Google Sheets and Forms with 100% success but some (like this one) just do not work to replace the header text only. I also double checked my Google Doc which correctly has {{QC}} and {{QD}} in the header section.
Any help would be appreciated.
function autofillForm(e) {
var timestamp = e.values[0];
var timeslot = e.values[1];
var QC = e.values[2];
var QD = e.values[3];
var QE = e.values[4];
var QF = e.values[5];
var QG = e.values[6];
var QH = e.values[7];
var QI = e.values[8];
var QJ = e.values[9];
var QK = e.values[10];
var QL = e.values[11];
var QM = e.values[12];
var QN = e.values[13];
var QO = e.values[14];
var QP = e.values[15];
var QQ = e.values[16];
var QR = e.values[17];
var QS = e.values[18];
var QT = e.values[19];
var QU = e.values[20];
var QV = e.values[21];
var QW = e.values[22];
var QX = e.values[23];
var QY = e.values[24];
var QZ = e.values[25];
var QAA = e.values[26];
var QAB = e.values[27];
var QAC = e.values[28];
var QAD = e.values[29];
var QAE = e.values[30];
var QAF = e.values[31];
var QAG = e.values[32];
var QAH = e.values[33];
var QAI = e.values[34];
var QAJ = e.values[35];
var QAK = e.values[36];
var QAL = e.values[37];
var QAM = e.values[38];
var QAN = e.values[39];
var QAO = e.values[40];
var QAP = e.values[41];
var QAQ = e.values[42];
var QAR = e.values[43];
var QAS = e.values[44];
var QAT = e.values[45];
var QAU = e.values[46];
var QAV = e.values[47];
var QAW = e.values[48];
var QAX = e.values[49];
var QAY = e.values[50];
var QAZ = e.values[51];
var QBA = e.values[52];
var QBB = e.values[53];
var QBC = e.values[54];
var QBD = e.values[55];
var QBE = e.values[56];
var QBF = e.values[57];
var QBG = e.values[58];
var QBH = e.values[59];
var QBI = e.values[60];
var QBJ = e.values[61];
var QBK = e.values[62];
var QBL = e.values[63];
var QBM = e.values[64];
var QBN = e.values[65];
var QBO = e.values[66];
var QBP = e.values[67];
var QBQ = e.values[68];
var QBR = e.values[69];
var QBS = e.values[70];
var QBT = e.values[71];
var QBU = e.values[72];
var QBV = e.values[73];
var QBW = e.values[74];
var QBX = e.values[75];
var QBY = e.values[76];
var QBZ = e.values[77];
var QCA = e.values[78];
var QCB = e.values[79];
var QCC = e.values[80];
var QCD = e.values[81];
var QCE = e.values[82];
var QCF = e.values[83];
var QCG = e.values[84];
var QCH = e.values[85];
var QCI = e.values[86];
var QCJ = e.values[87];
var QCK = e.values[88];
var QCL = e.values[89];
var QCM = e.values[90];
var QCN = e.values[91];
var QCO = e.values[92];
var QCP = e.values[93];
var QCQ = e.values[94];
var QCR = e.values[95];
var QCS = e.values[96];
var QCT = e.values[97];
var QCU = e.values[98];
var QCV = e.values[99];
var QCW = e.values[100];
var QCX = e.values[101];
var QCY = e.values[102];
var QCZ = e.values[103];
var QDA = e.values[104];
var QDB = e.values[105];
var QDC = e.values[106];
var QDD = e.values[107];
var QDE = e.values[108];
var QDF = e.values[119];
var QDG = e.values[110];
var QDH = e.values[111];
var QDI = e.values[112];
var QDJ = e.values[113];
var QDK = e.values[114];
var QDL = e.values[115];
var QDM = e.values[116];
var QDN = e.values[117];
var QDO = e.values[118];
var QDP = e.values[119];
var QDQ = e.values[120];
var QDR = e.values[121];
var QDS = e.values[122];
var QDT = e.values[123];
var QDU = e.values[124];
var QDV = e.values[125];
var QDW = e.values[126];
var QDX = e.values[127];
var QDY = e.values[128];
var QDZ = e.values[129];
var QEA = e.values[130];
var QEB = e.values[131];
var QEC = e.values[132];
var QED = e.values[133];
var QEE = e.values[134];
var QEF = e.values[135];
var QEG = e.values[136];
var QEH = e.values[137];
var QEI = e.values[138];
var QEJ = e.values[139];
var QEK = e.values[140];
var QEL = e.values[141];
var QEM = e.values[142];
var QEN = e.values[143];
var QEO = e.values[144];
var QEP = e.values[145];
var QEQ = e.values[146];
var QER = e.values[147];
var QES = e.values[148];
var QET = e.values[149];
var QEU = e.values[150];
var QEV = e.values[151];
var QEW = e.values[152];
var QEX = e.values[153];
var QEY = e.values[154];
var QEZ = e.values[155];
var QFA = e.values[156];
var QFB = e.values[157];
var QFC = e.values[158];
var QFD = e.values[159];
var QFE = e.values[160];
var QFF = e.values[161];
var QFG = e.values[162];
var QFH = e.values[163];
var QFI = e.values[164];
var QFJ = e.values[165];
var QFK = e.values[166];
var QFL = e.values[167];
var QFM = e.values[168];
var QFN = e.values[169];
var QFO = e.values[170];
var QFP = e.values[171];
var QFQ = e.values[172];
var QFR = e.values[173];
var QFS = e.values[174];
var QFT = e.values[175];
var QFU = e.values[176];
var QFV = e.values[177];
var QFW = e.values[178];
var QFX = e.values[179];
var QFY = e.values[180];
var QFZ = e.values[181];
var QGA = e.values[182];
var QGB = e.values[183];
var QGC = e.values[184];
var QGD = e.values[185];
var QGE = e.values[186];
var QGF = e.values[187];
var QGG = e.values[188];
var QGH = e.values[189];
var QGI = e.values[190];
var QGJ = e.values[191];
var QGK = e.values[192];
var QGL = e.values[193];
var QGM = e.values[194];
var QGN = e.values[195];
var QGO = e.values[196];
var QGP = e.values[197];
var QGQ = e.values[198];
var QGR = e.values[199];
var QGS = e.values[200];
var QGT = e.values[201];
var QGU = e.values[202];
var QGV = e.values[203];
var QGW = e.values[204];
var QGX = e.values[205];
var QGY = e.values[206];
var QGZ = e.values[207];
var QHA = e.values[208];
var QHB = e.values[209];
var QHC = e.values[210];
var QHD = e.values[211];
var QHE = e.values[212];
var QHF = e.values[213];
var QHG = e.values[214];
var QHH = e.values[215];
var QHI = e.values[216];
var QHJ = e.values[217];
var QHK = e.values[218];
var QHL = e.values[219];
var QHM = e.values[220];
var QHN = e.values[221];
var QHO = e.values[222];
var QHP = e.values[223];
var QHQ = e.values[224];
var QHR = e.values[225];
var QHS = e.values[226];
var QHT = e.values[227];
var QHU = e.values[228];
var QHV = e.values[229];
var QHW = e.values[230];
var QHX = e.values[231];
var QHY = e.values[232];
var QHZ = e.values[233];
var QIA = e.values[234];
var QIB = e.values[235];
var QIC = e.values[236];
var QID = e.values[237];
var QIE = e.values[238];
var QIF = e.values[239];
var QIG = e.values[240];
var QIH = e.values[241];
var QII = e.values[242];
var QIJ = e.values[243];
var QIK = e.values[244];
var QIL = e.values[245];
var QIM = e.values[246];
var QIN = e.values[247];
var QIO = e.values[248];
var QIP = e.values[249];
var QIQ = e.values[250];
var QIR = e.values[251];
var QIS = e.values[252];
var QIT = e.values[253];
var QIU = e.values[254];
var QIV = e.values[255];
var QIW = e.values[256];
var QIX = e.values[257];
var QIY = e.values[258];
var QIZ = e.values[259];
var QJA = e.values[260];
var QJB = e.values[261];
var QJC = e.values[262];
var QJD = e.values[263];
var QJE = e.values[264];
var QJF = e.values[265];
var QJG = e.values[266];
var QJH = e.values[267];
var QJI = e.values[268];
var QJJ = e.values[269];
var QJK = e.values[270];
var QJL = e.values[271];
var QJM = e.values[272];
var QJN = e.values[273];
var QJO = e.values[274];
var QJP = e.values[275];
var templateFile = DriveApp.getFileById("XXX");
var templateResponseFolder = DriveApp.getFolderById("XXX");
var copy = templateFile.makeCopy(QF + QG + timeslot + QJP, templateResponseFolder);
var doc = DocumentApp.openById(copy.getId());
var body = doc.getBody();
var header = doc.getHeader();
header.replaceText("{{QC}}", QC);
header.replaceText("{{QD}}", QD);
body.replaceText("{{QE}}", QE);
body.replaceText("{{QF}}", QF);
body.replaceText("{{QG}}", QG);
body.replaceText("{{QH}}", QH);
body.replaceText("{{QI}}", QI);
body.replaceText("{{QJ}}", QJ);
body.replaceText("{{QK}}", QK);
body.replaceText("{{QL}}", QL);
body.replaceText("{{QM}}", QM);
body.replaceText("{{QN}}", QN);
body.replaceText("{{QO}}", QO);
body.replaceText("{{QP}}", QP);
body.replaceText("{{QQ}}", QQ);
body.replaceText("{{QR}}", QR);
body.replaceText("{{QS}}", QS);
body.replaceText("{{QT}}", QT);
body.replaceText("{{QU}}", QU);
body.replaceText("{{QV}}", QV);
body.replaceText("{{QW}}", QW);
body.replaceText("{{QX}}", QX);
body.replaceText("{{QY}}", QY);
body.replaceText("{{QZ}}", QZ);
body.replaceText("{{QAA}}", QAA);
body.replaceText("{{QAB}}", QAB);
body.replaceText("{{QAC}}", QAC);
body.replaceText("{{QAD}}", QAD);
body.replaceText("{{QAE}}", QAE);
body.replaceText("{{QAF}}", QAF);
body.replaceText("{{QAG}}", QAG);
body.replaceText("{{QAH}}", QAH);
body.replaceText("{{QAI}}", QAI);
body.replaceText("{{QAJ}}", QAJ);
body.replaceText("{{QAK}}", QAK);
body.replaceText("{{QAL}}", QAL);
body.replaceText("{{QAM}}", QAM);
body.replaceText("{{QAN}}", QAN);
body.replaceText("{{QAO}}", QAO);
body.replaceText("{{QAP}}", QAP);
body.replaceText("{{QAQ}}", QAQ);
body.replaceText("{{QAR}}", QAR);
body.replaceText("{{QAS}}", QAS);
body.replaceText("{{QAT}}", QAT);
body.replaceText("{{QAU}}", QAU);
body.replaceText("{{QAV}}", QAV);
body.replaceText("{{QAW}}", QAW);
body.replaceText("{{QAX}}", QAX);
body.replaceText("{{QAY}}", QAY);
body.replaceText("{{QAZ}}", QAZ);
body.replaceText("{{QBA}}", QBA);
body.replaceText("{{QBB}}", QBB);
body.replaceText("{{QBC}}", QBC);
body.replaceText("{{QBD}}", QBD);
body.replaceText("{{QBE}}", QBE);
body.replaceText("{{QBF}}", QBF);
body.replaceText("{{QBG}}", QBG);
body.replaceText("{{QBH}}", QBH);
body.replaceText("{{QBI}}", QBI);
body.replaceText("{{QBJ}}", QBJ);
body.replaceText("{{QBK}}", QBK);
body.replaceText("{{QBL}}", QBL);
body.replaceText("{{QBM}}", QBM);
body.replaceText("{{QBN}}", QBN);
body.replaceText("{{QBO}}", QBO);
body.replaceText("{{QBP}}", QBP);
body.replaceText("{{QBQ}}", QBQ);
body.replaceText("{{QBR}}", QBR);
body.replaceText("{{QBS}}", QBS);
body.replaceText("{{QBT}}", QBT);
body.replaceText("{{QBU}}", QBU);
body.replaceText("{{QBV}}", QBV);
body.replaceText("{{QBW}}", QBW);
body.replaceText("{{QBX}}", QBX);
body.replaceText("{{QBY}}", QBY);
body.replaceText("{{QBZ}}", QBZ);
body.replaceText("{{QCA}}", QCA);
body.replaceText("{{QCB}}", QCB);
body.replaceText("{{QCC}}", QCC);
body.replaceText("{{QCD}}", QCD);
body.replaceText("{{QCE}}", QCE);
body.replaceText("{{QCF}}", QCF);
body.replaceText("{{QCG}}", QCG);
body.replaceText("{{QCH}}", QCH);
body.replaceText("{{QCI}}", QCI);
body.replaceText("{{QCJ}}", QCJ);
body.replaceText("{{QCK}}", QCK);
body.replaceText("{{QCL}}", QCL);
body.replaceText("{{QCM}}", QCM);
body.replaceText("{{QCN}}", QCN);
body.replaceText("{{QCO}}", QCO);
body.replaceText("{{QCP}}", QCP);
body.replaceText("{{QCQ}}", QCQ);
body.replaceText("{{QCQ}}", QCQ);
body.replaceText("{{QCR}}", QCR);
body.replaceText("{{QCS}}", QCS);
body.replaceText("{{QCT}}", QCT);
body.replaceText("{{QCU}}", QCU);
body.replaceText("{{QCV}}", QCV);
body.replaceText("{{QCW}}", QCW);
body.replaceText("{{QCX}}", QCX);
body.replaceText("{{QCY}}", QCY);
body.replaceText("{{QCZ}}", QCZ);
body.replaceText("{{QDA}}", QDA);
body.replaceText("{{QDB}}", QDB);
body.replaceText("{{QDC}}", QDC);
body.replaceText("{{QDD}}", QDD);
body.replaceText("{{QDE}}", QDE);
body.replaceText("{{QDF}}", QDF);
body.replaceText("{{QDG}}", QDG);
body.replaceText("{{QDH}}", QDH);
body.replaceText("{{QDI}}", QDI);
body.replaceText("{{QDJ}}", QDJ);
body.replaceText("{{QDK}}", QDK);
body.replaceText("{{QDL}}", QDL);
body.replaceText("{{QDM}}", QDM);
body.replaceText("{{QDN}}", QDN);
body.replaceText("{{QDO}}", QDO);
body.replaceText("{{QDP}}", QDP);
body.replaceText("{{QDQ}}", QDQ);
body.replaceText("{{QDR}}", QDR);
body.replaceText("{{QDS}}", QDS);
body.replaceText("{{QDT}}", QDT);
body.replaceText("{{QDU}}", QDU);
body.replaceText("{{QDV}}", QDV);
body.replaceText("{{QDW}}", QDW);
body.replaceText("{{QDX}}", QDX);
body.replaceText("{{QDY}}", QDY);
body.replaceText("{{QDZ}}", QDZ);
body.replaceText("{{QEA}}", QEA);
body.replaceText("{{QEB}}", QEB);
body.replaceText("{{QEC}}", QEC);
body.replaceText("{{QED}}", QED);
body.replaceText("{{QEE}}", QEE);
body.replaceText("{{QEF}}", QEF);
body.replaceText("{{QEG}}", QEG);
body.replaceText("{{QEH}}", QEH);
body.replaceText("{{QEI}}", QEI);
body.replaceText("{{QEJ}}", QEJ);
body.replaceText("{{QEK}}", QEK);
body.replaceText("{{QEL}}", QEL);
body.replaceText("{{QEM}}", QEM);
body.replaceText("{{QEN}}", QEN);
body.replaceText("{{QEO}}", QEO);
body.replaceText("{{QEP}}", QEP);
body.replaceText("{{QEQ}}", QEQ);
body.replaceText("{{QER}}", QER);
body.replaceText("{{QES}}", QES);
body.replaceText("{{QET}}", QET);
body.replaceText("{{QEU}}", QEU);
body.replaceText("{{QEV}}", QEV);
body.replaceText("{{QEW}}", QEW);
body.replaceText("{{QEX}}", QEX);
body.replaceText("{{QEY}}", QEY);
body.replaceText("{{QEZ}}", QEZ);
body.replaceText("{{QFA}}", QFA);
body.replaceText("{{QFB}}", QFB);
body.replaceText("{{QFC}}", QFC);
body.replaceText("{{QFD}}", QFD);
body.replaceText("{{QFE}}", QFE);
body.replaceText("{{QFF}}", QFF);
body.replaceText("{{QFG}}", QFG);
body.replaceText("{{QFH}}", QFH);
body.replaceText("{{QFI}}", QFI);
body.replaceText("{{QFJ}}", QFJ);
body.replaceText("{{QFK}}", QFK);
body.replaceText("{{QFL}}", QFL);
body.replaceText("{{QFM}}", QFM);
body.replaceText("{{QFN}}", QFN);
body.replaceText("{{QFO}}", QFO);
body.replaceText("{{QFP}}", QFP);
body.replaceText("{{QFQ}}", QFQ);
body.replaceText("{{QFR}}", QFR);
body.replaceText("{{QFS}}", QFS);
body.replaceText("{{QFT}}", QFT);
body.replaceText("{{QFU}}", QFU);
body.replaceText("{{QFV}}", QFV);
body.replaceText("{{QFW}}", QFW);
body.replaceText("{{QFX}}", QFX);
body.replaceText("{{QFY}}", QFY);
body.replaceText("{{QFZ}}", QFZ);
body.replaceText("{{QGA}}", QGA);
body.replaceText("{{QGB}}", QGB);
body.replaceText("{{QGC}}", QGC);
body.replaceText("{{QGD}}", QGD);
body.replaceText("{{QGE}}", QGE);
body.replaceText("{{QGF}}", QGF);
body.replaceText("{{QGG}}", QGG);
body.replaceText("{{QGH}}", QGH);
body.replaceText("{{QGI}}", QGI);
body.replaceText("{{QGJ}}", QGJ);
body.replaceText("{{QGK}}", QGK);
body.replaceText("{{QGL}}", QGL);
body.replaceText("{{QGM}}", QGM);
body.replaceText("{{QGN}}", QGN);
body.replaceText("{{QGO}}", QGO);
body.replaceText("{{QGP}}", QGP);
body.replaceText("{{QGQ}}", QGQ);
body.replaceText("{{QGR}}", QGR);
body.replaceText("{{QGS}}", QGS);
body.replaceText("{{QGT}}", QGT);
body.replaceText("{{QGU}}", QGU);
body.replaceText("{{QGV}}", QGV);
body.replaceText("{{QGW}}", QGW);
body.replaceText("{{QGX}}", QGX);
body.replaceText("{{QGY}}", QGY);
body.replaceText("{{QGZ}}", QGZ);
body.replaceText("{{QHA}}", QHA);
body.replaceText("{{QHB}}", QHB);
body.replaceText("{{QHC}}", QHC);
body.replaceText("{{QHD}}", QHD);
body.replaceText("{{QHE}}", QHE);
body.replaceText("{{QHF}}", QHF);
body.replaceText("{{QHG}}", QHG);
body.replaceText("{{QHH}}", QHH);
body.replaceText("{{QHI}}", QHI);
body.replaceText("{{QHJ}}", QHJ);
body.replaceText("{{QHK}}", QHK);
body.replaceText("{{QHL}}", QHL);
body.replaceText("{{QHM}}", QHM);
body.replaceText("{{QHN}}", QHN);
body.replaceText("{{QHO}}", QHO);
body.replaceText("{{QHP}}", QHP);
body.replaceText("{{QHQ}}", QHQ);
body.replaceText("{{QHR}}", QHR);
body.replaceText("{{QHS}}", QHS);
body.replaceText("{{QHT}}", QHT);
body.replaceText("{{QHU}}", QHU);
body.replaceText("{{QHV}}", QHV);
body.replaceText("{{QHW}}", QHW);
body.replaceText("{{QHX}}", QHX);
body.replaceText("{{QHY}}", QHY);
body.replaceText("{{QHZ}}", QHZ);
body.replaceText("{{QIA}}", QIA);
body.replaceText("{{QIB}}", QIB);
body.replaceText("{{QIC}}", QIC);
body.replaceText("{{QID}}", QID);
body.replaceText("{{QIE}}", QIE);
body.replaceText("{{QIF}}", QIF);
body.replaceText("{{QIG}}", QIG);
body.replaceText("{{QIH}}", QIH);
body.replaceText("{{QII}}", QII);
body.replaceText("{{QIJ}}", QIJ);
body.replaceText("{{QIK}}", QIK);
body.replaceText("{{QIL}}", QIL);
body.replaceText("{{QIM}}", QIM);
body.replaceText("{{QIN}}", QIN);
body.replaceText("{{QIO}}", QIO);
body.replaceText("{{QIP}}", QIP);
body.replaceText("{{QIQ}}", QIQ);
body.replaceText("{{QIR}}", QIR);
body.replaceText("{{QIS}}", QIS);
body.replaceText("{{QIT}}", QIT);
body.replaceText("{{QIU}}", QIU);
body.replaceText("{{QIV}}", QIV);
body.replaceText("{{QIW}}", QIW);
body.replaceText("{{QIX}}", QIX);
body.replaceText("{{QIY}}", QIY);
body.replaceText("{{QIZ}}", QIZ);
body.replaceText("{{QJA}}", QJA);
body.replaceText("{{QJB}}", QJB);
body.replaceText("{{QJC}}", QJC);
body.replaceText("{{QJD}}", QJD);
body.replaceText("{{QJE}}", QJE);
body.replaceText("{{QJF}}", QJF);
body.replaceText("{{QJG}}", QJG);
body.replaceText("{{QJH}}", QJH);
body.replaceText("{{QJI}}", QJI);
body.replaceText("{{QJJ}}", QJJ);
body.replaceText("{{QJK}}", QJK);
body.replaceText("{{QJL}}", QJL);
body.replaceText("{{QJM}}", QJM);
body.replaceText("{{QJN}}", QJN);
body.replaceText("{{QJO}}", QJO);
body.replaceText("{{QJP}}", QJP);
doc.saveAndClose();
}
Related
Script returning no results for data collection in an API
When trying to copy the API data to the spreadsheet returns blank, I would like to know where I am failing in the script (I believe it is something simple but I am letting go without seeing it) function CartolaMarket() { var url = 'https://api.cartolafc.globo.com/atletas/mercado'; var response = UrlFetchApp.fetch(url); var results = JSON.parse(response.getContentText()); var table = [['apelido','clube_id','posicao_id','status_id','media_num']]; for (var i = 0; i < results.length; i++) { var r = results[i]; var apelido = r.atletas.apelido; var clube_id = r.atletas.clube_id; var posicao_id = r.atletas.posicao_id; var status_id = r.atletas.status_id; var media_num = r.atletas.media_num; table.push([apelido,clube_id,posicao_id,status_id,media_num]); } var sheet = SpreadsheetApp.getActiveSheet(); sheet.getRange(1,1, table.length, table[0].length).setValues(table); } Return Data API: Link to Spreadsheet: https://docs.google.com/spreadsheets/d/1R9Naeuvl7ZequmyNwMOR7z_qKWfynxFa7vb6dq9RysU/edit?usp=sharing Data I intend to collect: apelido clube_id posicao_id status_id media_num API Link: https://api.cartolafc.globo.com/atletas/mercado
It's not quite an answer, just a hint how you could fix it (I hope). Try this: var r = results[i].atletas[0]; var apelido = r.apelido; var clube_id = r.clube_id; var posicao_id = r.posicao_id; var status_id = r.status_id; var media_num = r.media_num; or this: var r = results[i]; var apelido = r.atletas[0].apelido; var clube_id = r.atletas[0].clube_id; var posicao_id = r.atletas[0].posicao_id; var status_id = r.atletas[0].status_id; var media_num = r.atletas[0].media_num; Instead of this: var r = results[i]; var apelido = r.atletas.apelido; var clube_id = r.atletas.clube_id; var posicao_id = r.atletas.posicao_id; var status_id = r.atletas.status_id; var media_num = r.atletas.media_num; Since, as you were told, the atletas is an array. You must iterate though it, or take its first element atletas[0], if it's enough. Try this: function CartolaMarket() { var url = 'https://api.cartolafc.globo.com/atletas/mercado'; var response = UrlFetchApp.fetch(url); var results = JSON.parse(response.getContentText()); // loop trough 'atletas', not through 'results' var atletas = results.atletas; var table = [['apelido','clube_id','posicao_id','status_id','media_num']]; for (var i = 0; i < atletas.length; i++) { var a = atletas[i]; var apelido = a.apelido; var clube_id = a.clube_id; var posicao_id = a.posicao_id; var status_id = a.status_id; var media_num = a.media_num; table.push([apelido,clube_id,posicao_id,status_id,media_num]); } var sheet = SpreadsheetApp.getActiveSheet(); sheet.getRange(1,1, table.length, table[0].length).setValues(table); } But I haven't tested this. Looks like it works.
My Code Returns the correct Row (i think) but read writes to the wrong row Google Sheets Script
I'm trying to: Find the Row number the Value from Cell 'PENALTY LINEUP ENTRY'!B10 on the sheet called Penalties Copy the Data for that person 'PENALTY LINEUP ENTRY'!C10:J11 to the adjacent cells on the Penalties sheet. The problem is that the data gets written to the wrong row and I don't know why. Here is my spreadsheet: https://docs.google.com/spreadsheets/d/1aKcfX4u91aYFMPMOJBSlx92JmhRGH3MeVjLenxbEtAw/edit?usp=sharing Below is my code: function next_jam() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheetByName('PENALTY LINEUP ENTRY'); var Psheet = ss.getSheetByName('Penalties'); var Jam = sheet.getRange('D5'); var JamValue = Jam.getValue(); var Period = sheet.getRange('D2').getValue(); var H_J =sheet.getRange('B10').getValue(); var H_P =sheet.getRange('B13').getValue(); var H_B1 =sheet.getRange('B16').getValue(); var H_B2 =sheet.getRange('B19').getValue(); var H_B3 =sheet.getRange('B22').getValue(); var A_J =sheet.getRange('N10').getValue(); var A_P =sheet.getRange('N13').getValue(); var A_B1 =sheet.getRange('N16').getValue(); var A_B2 =sheet.getRange('N19').getValue(); var A_B3 =sheet.getRange('N22').getValue(); var H_Jp = sheet.getRange('C10:J11'); var H_JpValues = H_Jp.getValues(); var H_Pp = sheet.getRange('C13:J14'); var H_PpValues = H_Pp.getValues(); var H_B1p = sheet.getRange('C16:J17'); var H_B1pValues = H_B1p.getValues(); var H_B2p = sheet.getRange('C19:J20'); var H_B2pValues = H_B2p.getValues(); var H_B3p = sheet.getRange('C22:J23'); var H_B3pValues = H_B3p.getValues(); var A_Jp = sheet.getRange('O10:V11'); var A_JpValues = A_Jp.getValues(); var A_Pp = sheet.getRange('O13:V14'); var A_PpValues = A_Pp.getValues(); var A_B1p = sheet.getRange('O16:V17'); var A_B1pValues = A_B1p.getValues(); var A_B2p = sheet.getRange('O19:V20'); var A_B2pValues = A_B2p.getValues(); var A_B3p = sheet.getRange('O22:V32') var A_B3pValues = A_B3p.getValues(); var H_P1 = Psheet.getRange('A1:H43'); var H_P2 = Psheet.getRange('AC1:AJ43'); var A_P1 = Psheet.getRange('P1:W43'); var A_P2 = Psheet.getRange('AR1:AY43'); var H_P1values = H_P1.getValues(); var H_P2values = H_P2.getValues(); var A_P1values = A_P1.getValues(); var A_P2values = A_P2.getValues(); // var ss = SpreadsheetApp.getActiveSpreadsheet(); // var sheet = ss.getSheetByName('PENALTY LINEUP ENTRY'); // var Psheet = ss.getSheetByName('Penalties'); // var Jam = sheet.getRange('E5').getValue(); // var Period = sheet.getRange('E2').getValue(); // var H_J =sheet.getRange('B10').getValue(); // var H_Jp = sheet.getRange('C10:J11').getValues(); // var H_P1 = Psheet.getRange('A4:H43'); // var H_P1values = H_P1.getValues(); Logger.log('This is the CURRENT HOME JAMMER = '+H_J); for (var i = 0; i < 21; i++) { var row = ""; for (var j = 0; j < 21; j++) { if (H_P1values[i][j] == H_J && Period == 1) { row = H_P1values[i][j+1]; Logger.log('This is the Row the HOME JAMMER is on = '+i); // This is your row number
How to fix the issue mail merge issue
I am trying to create an approval flow using sheet mail merge with approval buttons in the mail . Mail could generate when the user submits the HTML form. Below script is not working I have attached the script with sample sheet. I am not able to identify the exact issue, please help me to fix or identify Sample Sheet var REQUESTS_STATE_COLUMN = 32; var STATE_APPROVERS_EMAILED = "EMAILED_Response Pending"; var STATE_APPROVED = "Approved"; var STATE_DENIED = "Rejected"; function sendEmail(e) { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = SpreadsheetApp.getActiveSheet(); var dataRange = sheet.getRange("A2:AH2"); var data = dataRange.getValues(); for (i in data) { var rowData = data[i]; var email = e.namedValues["Email Address"]; var emailAddress = rowData[30]; var submiterEmail = rowData[1]; var ProjectName = rowData[2]; var SrDesign = "Sr. Design Architect Services"; var PerHourRate = rowData[6]; var NumberOfHours = rowData[7]; var TotalRate = rowData[8]; var Contact = "Contact Centre Expert"; var PerHourRate2 = rowData[10]; var NumberOfHours2 = rowData[11]; var NTotalRate2 = rowData[12]; var NJrDesign = "Jr. Design Architect"; var PerHourRate3 = rowData[14]; var NumberOfHours3 = rowData[15]; var TotalRate3 = rowData[16]; var Outsourced = "Outsourced Technical Support Resource"; var PerHourRate4 = rowData[18]; var NumberOfHours4 = rowData[19]; var TotalRate4 = rowData[20]; var Supplier = "Supplier Management"; var PerHourRate5 = rowData[22]; var NumberOfHours5 = rowData[23]; var TotalRate5 = rowData[24]; var Project = "Project Management & Execution"; var PerHourRate6 = rowData[26]; var NumberOfHours6 = rowData[27]; var TotalRate6 = rowData[28]; var UserAccount = rowData[28]; var GrandTotalRate = rowData[29]; var seq = e.values[1]; var url = ScriptApp.getService().getUrl(); var sheet = SpreadsheetApp.openById('1i5x0oqpCDgOZO2Nbly4SpNkWpLbTXJhT6pPOfQ7NuEc').getSheetByName("Sheet1"); var cellval = range.getCell(row,1).getValue(); var options = '?approval=%APPROVE%&reply=%EMAIL%' .replace("%EMAIL%",e.namedValues["User Email Address"]) var approve = url+options.replace("%APPROVE%","Approved")+'&row='+data+'&cellval='+cellval; var reject = url+options.replace("%APPROVE%","Rejected")+'&row='+data+'&cellval='+cellval; var subject = 'Technology Charges for '+ProjectName+''; var html = '<p style="font-family:Verdana; fontsize=9px">Hello Recipient,<br/><br/><br/><br/>Referance No: '+cellval+'<br/><a href='+ approve +'> <b>approve </b></a><a href='+ reject +'> <b>Reject</b></a><br/>' MailApp.sendEmail({ to: emailAddress, subject: subject, htmlBody: html, }); //Add a state in the form spreadsheet with APPROVERS_EMAILED state var sheet = SpreadsheetApp.getActiveSheet(); var lastRow = sheet.getLastRow(); sheet.getRange(lastRow, REQUESTS_STATE_COLUMN+1).setValue(STATE_APPROVERS_EMAILED); } } function doGet(e){ var answer = (e.parameter.approval === 'Approved') ? 'Yes -Approved!' : 'No-Rejected '; var cellval = e.parameter.cellval; var email = e.parameter.reply; var sheet = SpreadsheetApp.openById("1i5x0oqpCDgOZO2Nbly4SpNkWpLbTXJhT6pPOfQ7NuEc").getSheetByName("Sheet1"); var data = sheet.getDataRange().getValues(); var headers = data[0]; var approvalCol = headers.indexOf("Approval") + 1; if (0 === approvalCol) throw new Error ("Must add Approval column."); // Record approval or rejection in spreadsheet var row = ArrayLib.indexOf(data, 0, cellval); if (row < 0) throw new Error ("Request not available."); // Throw error if request was not found sheet.getRange(row+1, approvalCol).setValue(e.parameter.approval); // Display response to approver var app = UiApp.createApplication(); app.add(app.createHTML('<h2>Thank you..! Your response has been recorded'+' saying: '+ answer + '</h2>')) return app }
The getCell requires integer value for row and column but instead you are passing the entire row (rowData). You need to replace the line 45 to the below line. var cellval = dataRange.getCell(i+1, 1).getValue()
Saving files to a specified folder
I have a script that uses data from a form and key values to fill out a document and convert the document to a pdf. Now I am having trouble getting that pdf to save to a specified folder in my google drive. function myfunction(e) { var docTemplate = "1pKwW-RcjaVV8xmm00hWDx1u2QxzQwaSjPJwVY2ux2nI"; var docName = "Form"; var full_name = "Form"; var targetFolderId = "Work Order Forms"; function onFormSubmit(e) { var Assign_To_1 = e.value[2]; var Assign_To_2 = e.value[3]; var Timestamp = e.values[0]; var Date_Of_Request = e.value[1]; var Requested_By = e.value[7]; var Principal_Approval = e.value[5]; var Urgency = e.value[4]; var Building_Room_Number = e.value[10]; var Description_of_Work_To_Be_Done = e.value[6]; var Parts_Needed = e.value[8]; var Parts_In_Stock = e.value[9]; var Invoice_Number = e.value[10]; var Completed_Signature = e.value[22]; var Incomplete_Signature = e.value[23]; var On_Hold_Signature = e.value[24]; var Estimated_Hours = e.value[14]; var Number_Of_Employees_To_Complete = e.value[18]; var Budget = e.value[11]; var Follow_Up = e.value[16]; var Overtime = e.value[15]; var copyId = DocsList.getFileById(docTemplate) .makeCopy(docName+' for '+full_name) .getId(); var copyDoc = DocumentApp.openById(copyId); var copyBody = copyDoc.getActiveSection(); copyBody.replaceText('KeyAssignedTo1',Assign_To_1); copyBody.replaceText('KeyAssignedTo2',Assign_To_2); copyBody.replaceText('KeyTimestamp',Timestamp); copyBody.replaceText('KeyDateOfRequest',Date_Of_Request); copyBody.replaceText('KeyRequestedBy',Requested_By); copyBody.replaceText('KeyPrincipalApproval',Principal_Approval); copyBody.replaceText('KeyUrgency',Urgency); copyBody.replaceText('KeyBuildingRoomNumber',Building_Room_Number); copyBody.replaceText('KeyDescriptionofWorkToBeDone',Description_of_Work_To_Be_Done); copyBody.replaceText('KeyPartsNeeded', Parts_Needed); copyBody.replaceText('KeyPartsInStock', Parts_In_Stock); copyBody.replaceText('KeyInvoiceNumber', Invoice_Number); copyBody.replaceText('KeyCompletedSignature', Completed_Signature); copyBody.replaceText('KeyIncompleteSignature', Incomplete_Signature); copyBody.replaceText('KeyOnHoldSignature', On_Hold_Signature); copyBody.replaceText('KeyEstimatedHours', Estimated_Hours); copyBody.replaceText('KeyNumberOfEmployeesToComplete', Number_Of_Employees_To_Complete); copyBody.replaceText('KeyBudget', Budget); copyBody.replaceText('KeyFoolowUp', Follow_Up); copyBody.replaceText('KeyOvertime', Overtime); copyDoc.saveAndClose(); var pdf = DocsList.getFileById(copyId).getAs("application/pdf"); var doc = DocsList.getFileById(copyId); function moveFileToFolder(fileId, targetFolderId) { var targetFolder = DocsList.getFolderById(targetFolderId); var file = DocsList.getFileById(fileId); file.addToFolder(targetFolder); }}} Here is my code so far. Everything is working except the saving in a specified folder part. Please help I am very new to this.
See this old post : https://stackoverflow.com/questions/13532114/google-apps-script-addtofolder-adds-to-folder-and-mydrive/13536275#13536275 code is as follows : function moveFromRoot(){ folder=DocsList.createFolder("MyFolder"); var file=DocsList.createFile('File2', 'Empty'); file.addToFolder(folder); file.removeFromFolder(DocsList.getRootFolder()); } but as mentioned in the answer, you can create the file using the folder object itself (folder.createFile('blabla'))
Value e is not defined
I am trying to have a form fill out a doc using key values and then save the form for printing later in a folder in my drive. However the script keeps giving me the error "e" is not defined. I have no idea why. >function myfunction(e) { var docTemplate = "1pKwW-RcjaVV8xmm00hWDx1u2QxzQwaSjPJwVY2ux2nI"; var docName = "Form"; var full_name = "Form"; function onFormSubmit(e) {} var Assign_To_1 = e.value[2]; var Assign_To_2 = e.value[3]; var Timestamp = e.values[0]; var Date_Of_Request = e.value[1]; var Requested_By = e.value[7]; var Principal_Approval = e.value[5]; var Urgency = e.value[4]; var Building_Room_Number = e.value[10]; var Description_of_Work_To_Be_Done = e.value[6]; var Parts_Needed = e.value[8]; var Parts_In_Stock = e.value[9]; var Invoice_Number = e.value[10]; var Completed_Signature = e.value[22]; var Incomplete_Signature = e.value[23]; var On_Hold_Signature = e.value[24]; var Estimated_Hours = e.value[14]; var Number_Of_Employees_To_Complete = e.value[18]; var Budget = e.value[11]; var Follow_Up = e.value[16]; var Overtime = e.value[15]; var copyId = DocsList.getFileById(docTemplate) .makeCopy(docName+' for '+full_name) .getId(); var copyDoc = DocumentApp.openById(copyId); var copyBody = copyDoc.getActiveSection(); copyBody.replaceText('KeyAssignedTo1',Assign_To_1); copyBody.replaceText('KeyAssignedTo2',Assign_To_2); copyBody.replaceText('KeyTimestamp',Timestamp); copyBody.replaceText('KeyDateOfRequest',Date_Of_Request); copyBody.replaceText('KeyRequestedBy',Requested_By); copyBody.replaceText('KeyPrincipalApproval',Principal_Approval); copyBody.replaceText('KeyUrgency',Urgency); copyBody.replaceText('KeyBuildingRoomNumber',Building_Room_Number); copyBody.replaceText('KeyDescriptionofWorkToBeDone',Description_of_Work_To_Be_Done); copyBody.replaceText('KeyPartsNeeded', Parts_Needed); copyBody.replaceText('KeyPartsInStock', Parts_In_Stock); copyBody.replaceText('KeyInvoiceNumber', Invoice_Number); copyBody.replaceText('KeyCompletedSignature', Completed_Signature); copyBody.replaceText('KeyIncompleteSignature', Incomplete_Signature); copyBody.replaceText('KeyOnHoldSignature', On_Hold_Signature); copyBody.replaceText('KeyEstimatedHours', Estimated_Hours); copyBody.replaceText('KeyNumberOfEmployeesToComplete', Number_Of_Employees_To_Complete); copyBody.replaceText('KeyBudget', Budget); copyBody.replaceText('KeyFoolowUp', Follow_Up); copyBody.replaceText('KeyOvertime', Overtime); copyDoc.saveAndClose(); var pdf = DocsList.getFileById(copyId).getAs("application/pdf"); var doc = DocsList.getFileById(copyId); var folders = doc.getParents(); var newFolder=DocsList.getFolder(WorkOrderForms); doc.addToFolder(WorkOrderForms); var docParentFolder=folders[0]; doc.removeFromFolder(docParentFolder); } Please Help!
A few issues already solved in comments but I didn't see the biggest one at first : your main function is actually onFormSubmit but it is "hidden" in a myFunction function ... please rewrite it like below and setup your trigger to run onFormSubmit as the main function now has a different name... var docTemplate = "1pKwW-RcjaVV8xmm00hWDx1u2QxzQwaSjPJwVY2ux2nI"; var docName = "Form"; var full_name = "Form"; // you can keep these declarations as global variables function onFormSubmit(e) { // this is the real function starting point var Assign_To_1 = e.value[2]; var Assign_To_2 = e.value[3]; var Timestamp = e.values[0]; var Date_Of_Request = e.value[1]; var Requested_By = e.value[7]; var Principal_Approval = e.value[5]; var Urgency = e.value[4]; var Building_Room_Number = e.value[10]; var Description_of_Work_To_Be_Done = e.value[6]; var Parts_Needed = e.value[8]; var Parts_In_Stock = e.value[9]; var Invoice_Number = e.value[10]; var Completed_Signature = e.value[22]; var Incomplete_Signature = e.value[23]; var On_Hold_Signature = e.value[24]; var Estimated_Hours = e.value[14]; var Number_Of_Employees_To_Complete = e.value[18]; var Budget = e.value[11]; var Follow_Up = e.value[16]; var Overtime = e.value[15]; var copyId = DocsList.getFileById(docTemplate) .makeCopy(docName+' for '+full_name) .getId(); var copyDoc = DocumentApp.openById(copyId); var copyBody = copyDoc.getActiveSection(); copyBody.replaceText('KeyAssignedTo1',Assign_To_1); copyBody.replaceText('KeyAssignedTo2',Assign_To_2); copyBody.replaceText('KeyTimestamp',Timestamp); copyBody.replaceText('KeyDateOfRequest',Date_Of_Request); copyBody.replaceText('KeyRequestedBy',Requested_By); copyBody.replaceText('KeyPrincipalApproval',Principal_Approval); copyBody.replaceText('KeyUrgency',Urgency); copyBody.replaceText('KeyBuildingRoomNumber',Building_Room_Number); copyBody.replaceText('KeyDescriptionofWorkToBeDone',Description_of_Work_To_Be_Done); copyBody.replaceText('KeyPartsNeeded', Parts_Needed); copyBody.replaceText('KeyPartsInStock', Parts_In_Stock); copyBody.replaceText('KeyInvoiceNumber', Invoice_Number); copyBody.replaceText('KeyCompletedSignature', Completed_Signature); copyBody.replaceText('KeyIncompleteSignature', Incomplete_Signature); copyBody.replaceText('KeyOnHoldSignature', On_Hold_Signature); copyBody.replaceText('KeyEstimatedHours', Estimated_Hours); copyBody.replaceText('KeyNumberOfEmployeesToComplete', Number_Of_Employees_To_Complete); copyBody.replaceText('KeyBudget', Budget); copyBody.replaceText('KeyFoolowUp', Follow_Up); copyBody.replaceText('KeyOvertime', Overtime); copyDoc.saveAndClose(); var pdf = DocsList.getFileById(copyId).getAs("application/pdf"); var doc = DocsList.getFileById(copyId); var folders = doc.getParents(); var newFolder=DocsList.getFolder(WorkOrderForms); doc.addToFolder(WorkOrderForms); var docParentFolder=folders[0]; doc.removeFromFolder(docParentFolder); }