Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

javascript - I am basing a variable on a spreadsheet column to create PDFs in Google Apps Script. I can't get the variable to assign the correct value

I have a Google Form, and the results go to a Google Sheet. I wrote a Google Apps Script to create bulk PDFs from the spreadsheet.

There is a checkbox on the form, and the result that goes into the spreadsheet column is

Checked = "Check here if Information is same as above"

Unchecked = "" or NULL (I am not sure which)

This is the javascript to bulk create these PDFs. My problem is that it always comes through on the PDF as ?, even if there is nothing in that column. I have tried a multitude of way of putting it in different places, using brackets, etc. to no avail. I am a SQL gal, and this is literally the first javascript I have written, so any help is appreciated.

function CreateBulkPDFs() {
///////////begin function{

    const docFile = DriveApp.getFileById("1YAj3MubVRnYUpptEmc92esU0e3vP4KknrgvCO4l9BkI");
    const tempFolder = DriveApp.getFolderById("1gmCPUmXsl4iwaVWv-7l43-y82cz2ENfF");
    const pdfFolder = DriveApp.getFolderById("1UNHFAlvT5ZMTrIevdsIyf27SgyfO6PQQ");
    const currentSheet = SpreadsheetApp.openById("1oMmL0Wfl9FSFX7_xb4RH-qc2o4qJvhlkNMTKMkwHXB8").getSheetByName("Form Responses 1");
    const data = currentSheet.getRange(2, 1, currentSheet.getLastRow() - 1, 49).getDisplayValues();
    //getRange(row start, column start, # of rows, # of columns) 
    var chk;

  let errors = [];
  data.forEach(row => { 
  ///////////begin forEach2( 
   ///////////begin row2{
     try { 
     ///////////begin try{
  if (row[1] !="Success")
  if (row[28] = "Check here if Information is same as above") chk = "?";
  if (row[28] != "Check here if Information is same as above") chk = "?";
            CreatePDF(
                row[2],
                row[3],
                row[4],
                row[5],
                row[6],
                row[7],
                row[8],
                row[9],
                row[10],
                row[11],
                row[12],
                row[13],
                row[14],
                row[15],
                row[16],
                row[17],
                row[18],
                row[19],
                row[20],
                row[21],
                row[22],
                row[23],
                row[24],
                row[25],
                row[26],
                row[27],
                //This is Check here if Information is same as above - row[28]
                
                chk,
                
                row[29],
                row[30],
                row[31],
                row[32],
                row[33],
                row[34],
                row[35],
                row[36],
                row[37],
                row[38],
                row[39],
                row[40],
                row[41],
                row[42],
                row[43],
                row[44],
                row[45],
                row[46],
                row[47],
                row[48],
                row[2] + " DOB: " + row[3], /*Name of Document*/
                docFile,
                tempFolder,
                pdfFolder
            );
            errors.push(["Success"]);
        }
     ///////////end try{
catch (err) {
            errors.push(["Failed"]);
        }
       }
   ///////////end row {
    ); 
  ///////////end forEach(
    currentSheet.getRange(2, 2, currentSheet.getLastRow() - 1, 1).setValues(errors);
}
///////////end function{
question from:https://stackoverflow.com/questions/65849963/i-am-basing-a-variable-on-a-spreadsheet-column-to-create-pdfs-in-google-apps-scr

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your if statements are trying to assign a value (x = y) to row[28] rather than check for equality (x == y or in JavaScript, more typically x === y).

Try:

if (row[1] !== "Success")
if (row[28] === "Check here if Information is same as above") chk = "?";
if (row[28] !== "Check here if Information is same as above") chk = "?";

The === (rather than == as in most other languages) is a particular idiosyncrasy of JavaScript - you can go down that rabbit hole if you want, but it's usually safest to default to === to ensure you don't get unexpected results.

From the link above:

The strict equality operator (===) behaves identically to the abstract equality operator (==) except no type conversion is done, and the types must be the same to be considered equal. ... The == operator will compare for equality after doing any necessary type conversions. The === operator will not do the conversion, so if two values are not the same type === will simply return false. Both are equally quick.

Edit to add:

There may be an additional issue depending on your intention with the line if (row[1] !== "Success") that could give you confusing results - currently this condition will only apply to the line directly below (if (row[28] === "Check ...), but not the following line. In pseudocode that would look like:

if (x)
this line will execute conditionally
this line will not

So you might either want to remove it if it isn't necessary, or explicitly enclose the statements you want to execute conditionally within brackets

if (row[1] !== "Success") {
  if (row[28] === "Check here if Information is same as above") chk = "?";
  if (row[28] !== "Check here if Information is same as above") chk = "?";
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...