繁体   English   中英

我基于电子表格列中的变量在 Google Apps 脚本中创建 PDF。 我无法让变量分配正确的值

[英]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

我有一个谷歌表格,结果 go 到谷歌表格。 我编写了一个 Google Apps 脚本来从电子表格创建批量 PDF。

表单上有一个复选框,进入电子表格列的结果是

Checked = "如果信息与上述相同,请在此处检查"

未选中 = "" 或 NULL(我不确定哪个)

这是用于批量创建这些 PDF 的 javascript。 我的问题是它总是在 PDF 上以 ☒ 出现,即使该列中没有任何内容。 我尝试了多种方法将其放在不同的地方,使用括号等,但无济于事。 我是 SQL gal,这实际上是我写的第一个 javascript,所以任何帮助表示赞赏。

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{

您的if语句试图为row[28]分配一个值( x = y ),而不是检查是否相等( x == y或在 JavaScript 中,更典型的是x === y )。

尝试:

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 = "☐";

=== (而不是大多数其他语言中的== )是 JavaScript 的特殊特质 - 如果你愿意,你可以 go 进入那个兔子洞,但通常最安全的是默认为===以确保你不会得到意想不到的结果。

从上面的链接:

严格相等运算符 (===) 的行为与抽象相等运算符 (==) 相同,只是不进行类型转换,并且类型必须相同才能被视为相等。 ... == 运算符将在进行任何必要的类型转换后比较是否相等。 === 运算符不会进行转换,因此如果两个值的类型不同,=== 将简单地返回 false。 两者都同样快。

编辑添加

根据您对if (row[1] !== "Success")的意图,可能会有一个额外的问题,这可能会给您带来令人困惑的结果 - 目前此条件适用于正下方的行( if (row[28] === "Check... ),但不是以下行。在伪代码中看起来像:

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

因此,如果没有必要,您可能希望将其删除,或者将要有条件地执行的语句明确括在括号内

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 = "☐";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM