English 中文(简体)
页: 1
原标题:Google AppScript rowIndex variable is not passed to function

我有“来源”的地盘,希望将一些选定的数据移至“目标”的地盘。 功能是移动数据。

I m searching data in first column of "source" google sheet by providing "keyword". Then, row data of that "keyword" available will displayed. Then, move button clicked to move that data to Target sheet.

见以下守则。

法典


function searchData(keyword) {
  var sourceSheet = SpreadsheetApp.openById( SOURCE_SHEETID ).getSheetByName( dataSource );
  var data = sourceSheet.getDataRange().getValues();
  var matchingRows = [];

  for (var i = 0; i < data.length; i++) {
    if (data[i][0] === keyword) { // Assuming keyword is in the first column
      matchingRows.push(data[i]);
    }
  }

  return matchingRows;
}

function moveData(rowIndex) {
 if (rowIndex >= 0 && rowIndex < sourceSheet.getLastRow()) { 
  console.log( Moving data from rowIndex: , rowIndex);
  var sourceSpreadsheetId =  SOURCE_SHEETID ;
  var targetSpreadsheetId =  TARGET_SHEETID ;
  var sourceSheetName =  dataSource ;
  var targetSheetName =  dataTarget ;

  // Open the source and target spreadsheets
  var sourceSpreadsheet = SpreadsheetApp.openById(sourceSpreadsheetId);
  var targetSpreadsheet = SpreadsheetApp.openById(targetSpreadsheetId);

  // Get the source and target sheets
  var sourceSheet = sourceSpreadsheet.getSheetByName(sourceSheetName);
  var targetSheet = targetSpreadsheet.getSheetByName(targetSheetName);

  // Get the data to move based on rowIndex
  var dataToMove = sourceSheet.getRange(rowIndex + 1, 1, 1, sourceSheet.getLastColumn()).getValues();

  // Append the data to the target sheet
  targetSheet.appendRow(dataToMove[0]);

  // Delete the source row
  sourceSheet.deleteRow(rowIndex + 1); // Adjust the row number as needed
}
else {
    console.log( Invalid rowIndex: , rowIndex);
  }
}

指数.html

    function searchAndMoveData() {
      var keyword = document.getElementById( keyword ).value;
      google.script.run.withSuccessHandler(displaySearchResults).searchData(keyword);
    }

    function displaySearchResults(data) {
      var searchResultsDiv = document.getElementById( searchResults );
      searchResultsDiv.innerHTML =   ; // Clear previous results

      if (data.length === 0) {
        searchResultsDiv.innerHTML =  No matching data found. ;
      } else {
        var table =  <table> ;
        for (var i = 0; i < data.length; i++) {
          table +=  <tr><td>  + data[i].join( </td><td> ) +  </td><td><input type="button" value="Move" onclick="confirmMove(  + i +  )"></td></tr> ;
        }  
        table +=  </table> ;
        searchResultsDiv.innerHTML = table;
      }
    }

    function confirmMove(rowIndex) {
      if (confirm( Are you sure you want to move this data? )) {
        google.script.run.moveData(rowIndex);
      }
    }

问题在于我点击了去纽顿时,当选的拉帕塔没有向目标表移动。 资料来源:表。

I understood that the error is with the "rowIndex" I tried to find the error using console log. result: Invalid rowIndex: undefined

I think the issue is rowIndex variable is not being passed correctly to the moveData function, and it s undefined when the function is called. Need help to resolve the issue here.

我在此补充了样本表。 资料来源:https://docs.google.com/spreadsheets/d/1qJUY0ElRQvWdxn5vOdKKxwUBPHWztQ4gcS3qVy4/edit?usp=共享”rel=“nofollow noreferer” 职称=“Source 谷歌/url”>here和指标表

最佳回答

Modification points:

  • In your script, the row index is retrieved from the row values of matchingRows as confirmMove( + i + ). In this case, when all rows are retrieved, this can be used. But, in your situation, I m worried that the correct row index is not used because the row values are retrieved from not all rows. I think that this is the 1st issue.
  • In the function moveData(rowIndex), sourceSheet of if (rowIndex >= 0 && rowIndex < sourceSheet.getLastRow()) { is not declared. In your script, sourceSheet is declared in the if statement. I think that this is the 2nd issue.

当这些问题得到修改时,如何修改?

Google Apps Script side:

请用您的价值观取代<条码>。

function searchData(keyword) {
  var sourceSheet = SpreadsheetApp.openById( SOURCE_SHEETID ).getSheetByName( dataSource );
  var data = sourceSheet.getDataRange().getValues();
  var matchingRows = [];
  for (var i = 0; i < data.length; i++) {
    if (data[i][0] === keyword) {
      matchingRows.push({ data: data[i], rowIndex: i });
    }
  }
  return matchingRows;
}

function moveData(rowIndex) {
  var sourceSpreadsheetId =  SOURCE_SHEETID ;
  var sourceSheetName =  dataSource ;
  var sourceSpreadsheet = SpreadsheetApp.openById(sourceSpreadsheetId);
  var sourceSheet = sourceSpreadsheet.getSheetByName(sourceSheetName);
  if (rowIndex >= 0 && rowIndex < sourceSheet.getLastRow()) {
    console.log( Moving data from rowIndex: , rowIndex);
    var targetSpreadsheetId =  TARGET_SHEETID ;
    var targetSheetName =  dataTarget ;
    var targetSpreadsheet = SpreadsheetApp.openById(targetSpreadsheetId);
    var targetSheet = targetSpreadsheet.getSheetByName(targetSheetName);
    var dataToMove = sourceSheet.getRange(rowIndex + 1, 1, 1, sourceSheet.getLastColumn()).getValues();
    targetSheet.appendRow(dataToMove[0]);
    sourceSheet.deleteRow(rowIndex + 1);
  } else {
    console.log( Invalid rowIndex: , rowIndex);
  }
}

Javascript side:

Although I m not sure about your function confirm( Are you sure you want to move this data? ). So, in this modification, it is supposed that this function works fine. Please be careful about this.

function searchAndMoveData() {
  var keyword = document.getElementById( keyword ).value;
  google.script.run.withSuccessHandler(displaySearchResults).searchData(keyword);
}

function displaySearchResults(data) {
  var searchResultsDiv = document.getElementById( searchResults );
  searchResultsDiv.innerHTML =   ;
  if (data.length === 0) {
    searchResultsDiv.innerHTML =  No matching data found. ;
  } else {
    var table =  <table> ;
    for (var i = 0; i < data.length; i++) {
      table +=  <tr><td>  + data[i].data.join( </td><td> ) +  </td><td><input type="button" value="Move" onclick="confirmMove(  + data[i].rowIndex +  )"></td></tr> ;
    }
    table +=  </table> ;
    searchResultsDiv.innerHTML = table;
  }
}

function confirmMove(rowIndex) {
  if (confirm( Are you sure you want to move this data? )) {
    google.script.run.moveData(rowIndex);
  }
}

Note:

  • Unfortunately, I cannot know your actual situation. When you use your script as the Web Apps, please reflect the latest script to the Web Apps. Please be careful about this.

  • 当我用简单的读写法测试了这一经过修改的文字时,我确认该文字有效。 但是,如果文字与你的实际面表不正确操作,你是否能够提供样本面读物? 为此,我谨确认。

问题回答

暂无回答




相关问题
How to use year counter google app script?

I need an urgent help i have data as follows in a google sheet i need to add a year counter to the column d as follows using google apps script

Google Spreadsheet multiple column filter using OR

I have a Google Spreadsheet with 3 columns that are either blank or have a value. I want to get the count of the number of rows that has A and either B or C populated. If I were writing a SQL query ...

Run Google Apps Script on Google-Spreadsheet event?

If I create a Google apps script, can I hook it up to Google spreadsheet to run based on an event, or must I manually invoke it? I don t know if Google Sheets supports any events. For example: a ...

How do I insert a row in to a Google spreadsheet using c#

I ve seen; Accessing Google Spreadsheets with C# using Google Data API and http://code.google.com/apis/spreadsheets/data/2.0/developers_guide_dotnet.html#CreatingRows However i m still having ...

using Zend_Gdata_Spreadsheets for public spreadsheets?

I have this code which is working, to load a Google Spreadsheet and load some data from it. If the spreadsheet in question is public, how do i modify the code to not require a username/password? $key=...

Programatically updating a Google spreadsheet

I ve got a pre-existing Google spreadsheet. Each month I update this document. I ve got a template workseet in the spreadseet that I d like to clone and then update. I d prefer to clone the ...

热门标签