簡體   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