如何检查Google工作表列中是否存在值

时间:2017-07-09 07:50:07

标签: google-apps-script google-sheets

我已成功在Google网站中编写了一个应用程序脚本,该数据可以将数据输入Google Sheet,但我做了一些更改 的index.html:

<div>
    <form id="email_subscribe">
        <input type="text" name="email" id="email" placeholder="ادخل الاسم المقترح">
        <input type="submit" value="بحث">
    </form>
</div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>
    $( document ).ready(function() {
        $( "#email_subscribe" ).submit(function() {
            google.script.run.withSuccessHandler(function(ret){
                $( "#thank_you" ).show("slow");
                $("#email_subscribe").slideUp();
                console.log(ret);
            }).checkName(this); //"this" is the form element
        });
    });

</script>

code.gs

function doGet() {
    var html = HtmlService.createTemplateFromFile('index').evaluate()
    .setTitle('Web App').setSandboxMode(HtmlService
    .SandboxMode.NATIVE);
    return html;
}

function checkName(form){
    var ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/16-jpz1Xf-cbAshaIYMZpAkBflALw1YVu4m2hNHE-ZWQ/edit#gid=0').getSheetByName("names");
    var values = ss.getDataRange().getValues();
    for(n=0;n<values.length;++n){
        var cell = values[n][0] ; // 1 is the index of the column starting from 0
        console.log(cell);
    }
}

我的想法是,我想创建一个用户想要保留名称的简单系统,所以首先他检查它是否已经被保留,然后他继续保留它

1 个答案:

答案 0 :(得分:1)

我包含了一个名为Arraylib的库来操作数组 这就是code.gs文件对我有用的方式:

function doGet() {
var html = HtmlService.createTemplateFromFile('index').evaluate()
.setTitle('Web App').setSandboxMode(HtmlService
.SandboxMode.NATIVE);
return html;
 }

function checkName(form){
var newName = form.namee;
  var user = Session.getActiveUser().getEmail();
   var ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/16-jpz1Xf-cbAshaIYMZpAkBflALw1YVu4m2hNHE-ZWQ/edit#gid=0').getSheetByName('names');
   var range = ss.getRange('A1:A');
   var reserved = range.getValues();
   var ind = ArrayLib.countif(reserved,newName);
if (ind == 0 ){
ss.appendRow([newName, user]);
 }
}