0

私はかなり初心者のプログラマーです。 formSubmitイベントで簡単な「ステータス変更」をしようとしています。私の旅の助けや援助に感謝します。Googleシートのステータス列を変更する方法参照番号

ユーザーがフォーム(https://docs.google.com/a/gregbartonmba.com/forms/d/e/1FAIpQLSccMrgAWq95SOJO-1TC74lb4ri71aZzIWCMe342KmM66d6lqw/viewform)を入力すると、スクリプトが実行されます。 - ReferenceNumberを含む参照行を検索します(B列)。 - ステータス列(列A)の値を新しいステータスに変更します。

私は修正しようとしたいくつかのサンプルコードを持っていますが、それを正しく取得するにはあまりにも多くの初心者です。私は近くにいると思うが、いくらか失われている...ありがとう。

コードのシートをここで見つけることができます: 私のコーディングの試みは、以下の投稿https://docs.google.com/spreadsheets/d/1yXIggGwWg84CebcABTLxPcCd8YI7W7VJL7PmVkcjvJI/edit?usp=sharing

...

function changeStatus(e) { 
    var Status = e.values[0]; 
    var ReferenceNumber = e.values[1]; 


// Look up the cell in the sheet that contains the ReferenceNumber. 
// Find the instance in range B2:B that has the ReferenceNumber entered. 
// Then, change the code of the left-most cell (Status) to the new "Status"  (entered from form submission) 

/* Modified code from example at: 
http://stackoverflow.com/questions/18482143/google-scripts-search-spreadsheet-by-column-return-rows 
*/ 


var searchString = ReferenceNumber; 
var ss = SpreadsheetApp.openById("1yXIggGwWg84CebcABTLxPcCd8YI7W7VJL7PmVkcjvJI"); 
var column =1; //column Index 
var columnValues = ss.getRange(2, column, ss.getLastRow()).getValues(); //1st is header row 
var searchResult = columnValues.findIndex(searchString); //Row Index - 2 

if(searchResult != -1) 
{ 
    //searchResult + 2 is row index. 
      SpreadsheetApp.getActiveSpreadsheet().setActiveRange(ss.getRange(searchResult + 2, 3)).setValue("found here"); 
} 
} 

Array.prototype.findIndex = function(search){ 
    if(search == "") return false; 
    for (var i=0; i<this.length; i++) 
    if (this[i].toString().indexOf(search) > -1) return i; 

    return -1; 


    /* If reference number entered does not exist, show browser pop-up that states "Reference Number 
Does not exist. Please enter a valid reference number." 
    */ 


/* Catch any errors and then send them via email to [email protected] 
    Need error-catching in case the reference number does NOT exist in the 
    sheet. 
*/ 

} 

答えて

1

は、スプレッドシートのクラスはすべて1ベースのインデックスを使用することである第一の問題点ので、あなたは使用する必要があります

var column = 2; //column Index 

第2の問題は、スプレッドシート上getRangeを呼び出すようにしようとしているということです、あなたが必要です

var columnValues = ss.getSheetByName("MasterList").getRange(2, column, ss.getLastRow()).getValues(); 

第三の問題はあなたがString[][]オブジェクト、文字列の配列の配列にStringのfindIndexを実行していることです。
列ベクトルのみを持っていても、GASは2次元配列を使ってデータのレイアウトを明確にします。
正確に同じメモリ位置を参照しているため、[1] == [1]がfalseの場合、javascript配列は互いに等しいため、配列を詳細に確認する必要があります。その後、代わりにArray.prototype.findIndexレッツ・

Array.prototype.findIndexInColVector = function(search){ 
    if(search == "") return false; 
    for (var i = 0; i < this.length; i++) 
    if (this[i][0].toString().indexOf(search) > -1) return i; 
    return -1; 
} 

そして、私たちすることができます

var searchResult = columnValues.findIndexInColVector(searchString); //Row Index - 2 

第4の問題点、あなたは、スプレッドシートの範囲を選択しないようにしようとしている行を見つけましたシート

ss.getSheetByName("MasterList").getRange(searchResult + 2, 3).setValue("found here"); 

もちろん、状況を調整するには

ss.getSheetByName("MasterList").getRange(searchResult + 2, 1).setValue(Status); 
+0

ありがとうRobin!私は今日必要な修正をしようとします。あなたの答えと説明をありがとうございました。とても有難い! :) – GregB985

+0

ロビン、まず、あなたのご意見に感謝します。私はすでにたくさんのことを学んできました。まだ少し問題があります。私は問題が次のコード行に位置すると信じています。 var searchResult = columnValues.findIndex([searchString]);これは常に-1という値を生成します。結果として、ReferenceNumberのどの値がサブミットされても、セルA1の単語 "Status"が上書きされます。 Logger.Logコードを追加して、何が起きているのか理解しやすくなりました。あなたの助言のために立ってください。ありがとうございました。:) – GregB985

+0

配列でindexOfを実行することはできませんので、私の更新された答えを確認してください。 –

関連する問題