如果列有值,请______

时间:2017-12-02 22:53:01

标签: google-apps-script google-sheets

如何在工作表上的列中搜索值,如果值在那里继续代码?我正在尝试建立一个更新系统来检查他们的电子邮件是否在工作表上。如果他们的电子邮件在工作表上,那么它将检查他们的系统版本是否是最新的。如果系统不是最新的,它将复制他们的数据,删除他们当前的工作表,从示例创建一个副本,然后放入他们的数据。如果用户没有系统,它将为他们创建一个系统。它似乎不断跳过检查他们的电子邮件。

我的代码使用的是网址和ID,但出于显而易见的原因我将其删除了。下面是我的代码开头的示例。

function readlog()
  var user = Session.getEffectiveUser().getUsername();
  var email = Session.getEffectiveUser().getEmail();
  var ss = SpreadsheetApp.openByUrl(URL_);
  SpreadsheetApp.setActiveSpreadsheet(ss);
 var sheet = SpreadsheetApp.getActiveSpreadsheet()
 var sheet = sheet.getSheetByName('8th grade')
 var active = sheet.getRange('8th grade!b3:b').getValues()
 if (email == active) {
    //do something
 }

以下是我的代码的完整版本:

function readlog(){
   var user = Session.getEffectiveUser().getUsername();
   var email = Session.getEffectiveUser().getEmail();
   var timezone= "CST";
   var date = Utilities.formatDate(new Date(),timezone, "E MMM dd,yyyy HH:mm:ss");
   var ss = SpreadsheetApp.openByUrl(URL_);
  SpreadsheetApp.setActiveSpreadsheet(ss);
  var sheet = SpreadsheetApp.getActiveSpreadsheet()
  var sheet = sheet.getSheetByName('8th grade')
  var active = sheet.getRange('8th grade!b3:b').getValues()
  if (email == active) {
     var container = DriveApp.getFoldersByName('Reading Log')
     var fid = DriveApp.getFoldersByName('Reading Log').getId()
     var version = sheet.getRange('8th grade!a1')
     var update = container.getDescription();
     if (update == version) {
         var ui = SpreadsheetApp.getUi();
         var response = ui.alert('You already have a reading log, and you have the most recent update!', ui.ButtonSet.OK);

} else {
  var contents = container.getFiles();
  var files = DriveApp.getFolderById(fid).searchFiles(
 'title contains "Reading log"');
  while (files.hasNext()) {
    var file = files.next();
    var logtoupdate = file.getUrl;

  }

  var ss = SpreadsheetApp.openByUrl(logtoupdate);
  SpreadsheetApp.setActiveSpreadsheet(ss);
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var logrange = sheet.getRange('8th Grade!e5:o').getValues()
  SpreadsheetApp.getActiveSpreadsheet().deleteActiveSheet();
  var master = DriveApp.getFileById(URL_);
  var read = master.makeCopy("Reading Log",container);
  var target = read.getRange('8th Grade e5:o').setValues(logrange);
  var range = sheet.getRange("A1:B2");
  range.setValues([[50, 100]]);
  var ss = SpreadsheetApp.openByUrl(URL_);
  SpreadsheetApp.setActiveSpreadsheet(ss);
  var sheet = SpreadsheetApp.getActiveSpreadsheet()

}


} else {
var ss = SpreadsheetApp.openByUrl(URL_);
SpreadsheetApp.setActiveSpreadsheet(ss);
var sheet = SpreadsheetApp.getActiveSpreadsheet()
var targetFolder = DriveApp.createFolder("Reading Log");
var eighth = DriveApp.getFileById(URL_);
var share = eighth.makeCopy("(Name): 8th Grade Reading Log",targetFolder);
var master = DriveApp.getFileById(URL_);
var read = master.makeCopy("Reading Log",targetFolder);
var folder = sheet.getRange('8th grade!a1');
targetFolder.setDescription(folder);
var idsheet = read.getUrl();
var idshare = share.getUrl();
var idfolder = targetFolder.getUrl()
sheet.appendRow([user,email,date,idfolder,idsheet,idshare]);
var ui = SpreadsheetApp.getUi();
var response = ui.alert('Your Reading log is created. There is now a folder in your google drive called Reading log. Have Fun Reading!', ui.ButtonSet.OK);
  }
}

1 个答案:

答案 0 :(得分:0)

简短回答

而不是email == active使用active.join('|').split('|').indexOf(email)

解释

getValues()返回二维数组。

  • join('|')使用|作为分隔符将此数组转换为字符串。
  • split('|')将上述字符串转换为(一维)数组。
  • indexOf(email)返回上述数组中email值的索引。如果未找到email值,则会返回-1
  • 比较-1被解析为false