in Education by
I am trying to figure out how to pull only specific rows and columns using Google apps script. The =QUERY is not an option to use. Need to keep all of the logic in the script - my actual data set is quite large. To illustrate what I'm trying to solve. I have a little table of test data. TestData From which I only want columns 2,3,5 (zero based index) and only the rows with "fur". function testFour(sheetID, fromTabName, toTabName) { var sourceTab = SpreadsheetApp.openById(sheetID).getSheetByName(fromTabName); var values = sourceTab.getDataRange().getValues(); var columns = [2,3,5]; //only want these columns var output = new Array(); for (var i = 0; i < values.length; i++) { for (var j = 0; j < values[0].length; j++) { if (values[i][4] == "fur") { // only want these rows if (j in columns) { output.push(values[i][j]); } } } } var targetTab = SpreadsheetApp.openById(sheetID).getSheetByName(toTabName); targetTab.getRange(1, 1, output.length, columns.length).setValues(output); } This code bit works but it is not very maintainable if there are many columns: for(n=0; n < values.length; ++n) { var columns = [2,3,5]; if (values[n][4] === 'fur') { output.push( [values[n][2], values[n][3], values[n][5]]); } When I run the testFour function I get an error about "Cannot covert Array to Object[][] on this line of the code: targetTab.getRange(1, 1, output.length, columns.length).setValues(output); Appreciate any help. I was really surprised to not have found any examples of this. Every solution was using the =QUERY on the sheet. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Based on Cooper's suggestion above this was how I was able to get this to work: function testFourNew(sheetID, fromTabName, toTabName) { var sourceTab = SpreadsheetApp.openById(sheetID).getSheetByName(fromTabName); var targetTab = SpreadsheetApp.openById(sheetID).getSheetByName(toTabName); var srcrg = sourceTab.getDataRange(); var srcvA = srcrg.getValues(); var desvA=[]; // var columns = [2,3,5]; //only want these columns var columns = String("2,3,5").split(','); //only want these columns var tstVal = "fur"; var tstCol = 4; for (var i=0;i<srcvA.length;i++) { var tA=[]; if (srcvA[i][tstCol] ==tstVal) { for (var j=0;j<columns.length;j++) { //Logger.log(srcvA[i][columns[j]]); tA.push(srcvA[i][columns[j]]); } desvA.push(tA); } } targetTab.getRange(1, 1, desvA.length, desvA[0].length).setValues(desvA); } Thank you Cooper your direction and suggestions!

Related questions

0 votes
    Select the statement that has correct JavaScript syntax 1. document.write("text") 2. alert stop ; 3. console.log("text");...
asked Feb 23, 2021 by JackTerrance
0 votes
    I have added the Google Translate plugin to my web page. How can I get a callback to my ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I am looking for a solution that capitalizes each word of the input string to cell seperated by blank space ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 6, 2022 in Education by JackTerrance
0 votes
    I have the below data frame lst = [['A',1],['B',0],['C',1],['D',0],['E',1],['F',1],['G',1]] ... give all val with 1. df[df.val == 1] Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    Match column I with column II and select the correct answer using the codes given below the columns. Column - I Political ... 2, D - 4 Select the correct answer from above options...
asked Aug 3, 2022 in Education by JackTerrance
0 votes
    If I have a view in SQL which contains various computed columns, some of which could be very expensive, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    ___________ provides optional labels with the columns and rows. (a) Disnames (b) Dimnames (c) Denmes (d) ... Out of R Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    In _______________ index instead of storing all the columns for a record together, each column is stored ... topic in portion Indexing and Hashing of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    I found found several question's answers similar to this, I tried some of them, but no luck. May ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I am trying to use ES2017 async/await syntax with Babel. In package.json, I have "babel": { " ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    I am trying to use ES2017 async/await syntax with Babel. In package.json, I have "babel": { " ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    I found found several question's answers similar to this, I tried some of them, but no luck. May ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    I am trying to use ES2017 async/await syntax with Babel. In package.json, I have "babel": { " ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    Problem I have several tests that do the same thing in mocha. This for me, it's duplication, and is the ... ) you have dataProviders. In phpUnit a dataProvider works this way:...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    I have this JSON tree view that represents a menu : var menus = [ { label : "1", items: [ ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
...