Question

I have a spreadsheet in Google Docs. It has two sheets "projectList" and "projectListCompleted". When I change the contents of a specific column to "Completed" the code I have moves that ROW from projectList to projectListCompleted. If I go to the "Completed" sheet and change that same column to ANYTHING ELSE then it moves that ROW back to my Working Projects sheet. Well, I am trying to make the code a little more robust. I am creating a function called 'projectMover' and I want to call it and tell it was action to do... Completed, Cancelled, Active, etc. and have it make decisions based on that.

The code I use to move a project (i.e., an Active ROW) from one sheet to another is here:

function moveToNewSheet( myActiveSpreadsheet, myActiveSheet, myActiveRange, myTargetSheet )
{
  var row = myActiveRange.getRow();
  var numColumns = myActiveSheet.getLastColumn();
  var targetSheet = myActiveSpreadsheet.getSheetByName( myTargetSheet );
  var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
  myActiveSheet.getRange(row, 1, 1, numColumns).moveTo(target);
  myActiveSheet.deleteRow(row);
  // targetSheet.sort(1);
  return target;
};

This code works great. No problems.

But I'm starting on my projectMover function and hitting glitches because I don't understand the API as well as I would like. What I am trying to do is have 'projectMover' call the 'moveToNewSheet' code. This code passes back what SHOULD BE the RANGE of the moved project. I want to access COLUMN G in that range and insert the current date so that projects have a completed date. However, as you can see in my code I am not quite grasping how to access that cell.

Anyone have any ideas?

function projectMover( myActiveSpreadsheet, myActiveSheet, myActiveRange, myAction )
{

  switch ( myAction )
  {
    case "Completed":
      var myTarget = moveToNewSheet( myActiveSpreadsheet, myActiveSheet, myActiveRange, "projectListCompleted" );
      var targetCell = myTarget.getCell(1,7);
      targetCell.activate();
      targetCell.setValue( "test" );
      break;
    default:
      break;
  }

  // get targetSheet and sort it
};
Was it helpful?

Solution

Here is the code I used...

This function takes the currently ACTIVE sheet and the currently ACTIVE range of cells and moves them to where they need to go. In MY application each status type has a specified sheet it belongs on with most status' belonging in my projectStatus sheet. But, for instance, the COMPLETED status projects belong in projectCompleted sheet and the CANCELLED status projects belong in projectCancelled sheet. These connections are made in a separate sheet I call projectStatus with column 1 being the Status name, column 2 being the sheet that status belongs in, and column 3 is a derived number =COUNTIF(projectList!D:D,-insert status name here-) that counts up how many of those status' there are. I used this information in a chart.

function projectMove( myActiveSheet, myActiveRange)
{
  var curDate = Utilities.formatDate(new Date(), "CST", "MM/dd/yy hh:mm");
  var wrkSprsht = SpreadsheetApp.getActiveSpreadsheet();
  var wrkSht = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(myActiveSheet);
  var wrkRng = wrkSht.getRange(myActiveRange, 1, 1, 7);
  var wrkValues = wrkRng.getValues();

  var wrkProject = 
      {
        priority: wrkValues[0][0], 
        title: wrkValues[0][1], 
        resource: wrkValues[0][2], 
        status: wrkValues[0][3], 
        assigned: wrkValues[0][4],
        notes: wrkValues[0][5],
        dateTime: wrkValues[0][6],
        currentSheet: myActiveSheet,
        targetSheet: targetStatusSheet( wrkValues[0][3] )
      };

  Logger.log(wrkProject);
  var trgSht = wrkSprsht.getSheetByName( wrkProject.targetSheet );
  var trgRng = trgSht.getRange(trgSht.getLastRow() + 1, 1);
  wrkRng.moveTo(trgRng);
  wrkSht.deleteRow(wrkRng.getRow());
  var trgCell = trgSht.getRange(trgSht.getLastRow(),7);
  trgCell.setValue( curDate );  
  trgSht.sort(1);
};

It's the :

var wrkRng = wrkSht.getRange(myActiveRange, 1, 1, 7);
      var wrkValues = wrkRng.getValues();

      var wrkProject = 
          {
            priority: wrkValues[0][0], 
            title: wrkValues[0][1], 
            resource: wrkValues[0][2], 
            status: wrkValues[0][3], 
            assigned: wrkValues[0][4],
            notes: wrkValues[0][5],
            dateTime: wrkValues[0][6],
            currentSheet: myActiveSheet,
            targetSheet: targetStatusSheet( wrkValues[0][3] )
          };

The wrkValues[row][column] (a 2-dimensional array of values) accesses the individual cells as desired.

https://developers.google.com/apps-script/reference/spreadsheet/range#getValues()

Hope this helps someone else. I spent quite awhile looking...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top