我有“来源”的地盘,希望将一些选定的数据移至“目标”的地盘。 功能是移动数据。
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和指标表