Google 表格脚本的条件问题

Conditions problems for Google Sheets scripts

我不知道为什么我的条件不起作用,第一个条件总是给出真实的输出。我对此很陌生。

function Save() { 
  var Sheet = SpreadsheetApp.getActiveSpreadsheet();
  var shtinput = Sheet.getSheetByName('INPUT');
  var shtdb3 = Sheet.getSheetByName('MARET');
  var shtdb4 = Sheet.getSheetByName('APRIL');
  var shtdb5 = Sheet.getSheetByName('MEI');
  var shtdb6 = Sheet.getSheetByName('JUNI');
  var shtdb7 = Sheet.getSheetByName('JULI');
  var shtdb8 = Sheet.getSheetByName('AGUSTUS');
  var shtdb9 = Sheet.getSheetByName('SEPTEMBER');
  var shtdb10 = Sheet.getSheetByName('OKTOBER');
  var shtdb11 = Sheet.getSheetByName('NOVEMBER');
  var shtdb12 = Sheet.getSheetByName('DESEMBER');
  var nosheet = shtinput.getRange('E5').getValue();
  var pilihsheet = [];
  if (nosheet = 'JANUARI') {
    pilihsheet = Sheet.getSheetByName('JANUARI');
  } else if (nosheet = 'FEBRUARI') {
    pilihsheet = Sheet.getSheetByName('FEBRUARI');
  } else if (nosheet = 'MARET') {
    pilihsheet = Sheet.getSheetByName('MARET');
  } else if (nosheet = 'APRIL') {
    pilihsheet = Sheet.getSheetByName('APRIL');
  } else if (nosheet = 'MEI') {
    pilihsheet = Sheet.getSheetByName('MEI');
  } else if (nosheet = 'JUNI') {
    pilihsheet = Sheet.getSheetByName('JUNI');
  } else if (nosheet = 'JULI') {
    pilihsheet = Sheet.getSheetByName('JULI');
  } else if (nosheet = 'AGUSTUS') {
    pilihsheet = Sheet.getSheetByName('AGUSTUS');
  } else if (nosheet = 'SEPTEMBER') {
    pilihsheet = Sheet.getSheetByName('SEPTEMBER');
  } else if (nosheet = 'OKTOBER') {
    pilihsheet = Sheet.getSheetByName('OKTOBER');
  } else if (nosheet = 'NOVEMBER') {
    pilihsheet = Sheet.getSheetByName('NOVEMBER');
  } else if (nosheet = 'DESEMBER') {
    pilihsheet = Sheet.getSheetByName('DESEMBER');
  }               


  var tgl = shtinput.getRange('E7').getValue();
  var inout = shtinput.getRange('E9').getValue();
  var jumlah = shtinput.getRange('E11').getValue();
  var kategori = shtinput.getRange('E13').getValue();
  var keterangan = shtinput.getRange('E15').getValue();
  var sumber = shtinput.getRange('E17').getValue();

  var baris = pilihsheet.getRange('H3').getValue();
  baris += 1;
  var rangeisi = pilihsheet.getRange('A' + baris + ':F'+ baris);
  rangeisi.setValues([[tgl,inout,jumlah,kategori,keterangan,sumber]]);
  
  bersih();
}

function bersih() {
  var Sheet = SpreadsheetApp.getActiveSpreadsheet();
  var shtinput = Sheet.getSheetByName('Input');
  
  shtinput.getRange('E5').clearContent();
  shtinput.getRange('E9').clearContent();
  shtinput.getRange('E11').clearContent();
  shtinput.getRange('E13').clearContent();
  shtinput.getRange('E15').clearContent();
  shtinput.getRange('E17').clearContent();
  
}

在您的 IF 语句中,您实际上是在为变量 nosheet 赋值:

if (nosheet = 'JANUARI') {

改成这个,你应该没问题:

if (nosheet == 'JANUARI') {

有些人更喜欢使用“===”,它也比较数据类型

if (nosheet === 'JANUARI') {

此外,注意变量,它们区分大小写,大多数人使用驼峰式大小写来保持直白

所以代替:

var shtinput = Sheet.getSheetByName('INPUT');

考虑:

var shtInput = Sheet.getSheetByName('INPUT');