I want to write a script to read and log values of a specific row and copy them to a new sheet

女生的网名这么多〃 提交于 2021-02-08 12:10:31

问题


I want to build a script to automate creating a form out of specific values.

What I need to do is:

  • Ask the user which row is relevant for the form, enter the row in a text box
  • Log value of column A of that row (Date)
  • Check that row from column C-ZZZ for values (numbers)
  • if there is a cell with a value, log the value. If the cell is empty ignore it
  • If there is a value in a cell additionally log values of row 1-10 of that column (numbers + strings)
  • create a new sheet
  • copy logged values in a specific order into that new sheet (other order than in the first sheet)

I searched for scripts that offer any resemblance of what I want to do, but I only managed to copy values of a specific row and output them into a new sheet without any formatting

Ask user for a row (NaN error message doesn't work):

function tourzettelZeile() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var rowIdxStr = Browser.inputBox("For which row do you want to make the list?");
  if (rowIdxStr == 'cancel') {
    return;
  }
  var rowIdx = parseInt(rowIdxStr);
  if (rowIdx == NaN) {
    Browser.msgBox("Numbers only or 'cancel'");
    return;
  }  

}

My attempt at logging and pasting data:

function readData(spreadsheetID)
{
  var spreadsheetId = '1KdZNvKgwL6NMuF0FYGB8jBhRVpbwv954D_UcBZ22eh0';
  var plakatMenge = Sheets.Spreadsheets.Values.get(spreadsheetId, 'PlakatTool2019!DK15:DK900');
  var plakatFormat = Sheets.Spreadsheets.Values.get(spreadsheetId, 'PlakatTool2019!DK4');  
  var plakatName = Sheets.Spreadsheets.Values.get(spreadsheetId, 'PlakatTool2019!DK15:DK1');  
  var plakatiererName = Sheets.Spreadsheets.Values.get(spreadsheetId, 'PlakatTool2019!FK66');  
  var plakatInfo = Sheets.Spreadsheets.Values.get(spreadsheetId, 'PlakatTool2019!DK7');  
  var plakatGebiet = Sheets.Spreadsheets.Values.get(spreadsheetId, 'PlakatTool2019!FL66');   
  var auftragDatum = Sheets.Spreadsheets.Values.get(spreadsheetId, 'PlakatTool2019!A66');  
  Logger.log(plakatMenge.values);
  Logger.log(plakatFormat.values);
  Logger.log(plakatName.values);
  Logger.log(plakatiererName.values);
  Logger.log(plakatInfo.values);
  Logger.log(plakatGebiet.values);
  Logger.log(auftragDatum.values);
  Browser.msgBox(plakatMenge + plakatFormat + plakatName + plakatiererName + plakatInfo + plakatGebiet + auftragDatum);

  //Neues Sheet erstellen  
  var requests = [{
    'addSheet': {
      'properties': {
        'title': 'Tourzettel',
        'gridProperties': {
          'rowCount': 80,
          'columnCount': 14
        },
        'tabColor': {
          'red': 1.0,
          'green': 0.3,
          'blue': 0.4
        }
      }
    }
  }];

  var response =
      Sheets.Spreadsheets.batchUpdate({'requests': requests}, spreadsheetId);
  Logger.log('Created sheet with ID: ' +
      response.replies[0].addSheet.properties.sheetId);

  //writeValues
  // Specify some values to write to the sheet.
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  // ss is now the spreadsheet the script is associated with
  var sheet = ss.getSheets()[5];
var values = [
  [plakatMenge.values, "Gebiet", plakatFormat.values, plakatName.values, plakatiererName.values, plakatInfo.values, plakatGebiet.values, auftragDatum.values]
  ];
  var range = sheet.getRange("A1:H1");
  range.setValues(values);
}

Both these scripts don't do the right thing and are not combined (info on which row to check does nothing)


回答1:


This function attempts to do the first five items in your list: Feel free to use and debug it because I have not debugged it at all.

function readAndLog() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var row=SpreadsheetApp.getUi().prompt('Enter Row Number').getResponseText();
  if(typeof(row)!=Number) {
    throw("Invalid Response: terminating script");
    return;
  }else{
    Logger.log(sh.getRange(row,1).getValue());
    var vA=sh.getRange(row,3,1,sh.getLastColumn()-3).getValues();
    var rA=[];
    for(var i=0;i<vA.length;i++) {
      if(vA[0][1] && typeOf(vA[0][i])==Number) {
        rA.push({col:i+1,val:vA[0][i]});
        Logger.log('column: %s, value: %s',rA[i].col,rA[i].val);
        var cA=sh.getRange(1,rA[i].col,10,1).getValues();
        for(var j=0;j<cA.length;j++) {
          Logger.log('col: %s, row: %s value: %s',rA[i].col, j+1, cA[0][j]);
        }
      }
    }
  }
}

Thanks, that helped a lot. I had to get rid of this part:

 if(typeof(row)!=Number) {
        throw("Invalid Response: terminating script");
        return;
      }else{

As it gave me an error, I wasn't able to solve.

I added a bit to the code:

function Tourzettel(){
  readAndLog();
  newSheet();
  logOutput();
}


function readAndLog() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var row=SpreadsheetApp.getUi().prompt('Gib die Zeile mit dem Datum für die Tour ein').getResponseText();
    Logger.log(sh.getRange(row,1).getValue());
    var vA=sh.getRange(row,3,1,sh.getLastColumn()-3).getValues();
    var rA=[];
    for(var i=0;i<vA.length;i++) {
      if(vA[0][1] && typeOf(vA[0][i])==Number) {
        rA.push({col:i+1,val:vA[0][i]});
        Logger.log('column: %s, value: %s',rA[i].col,rA[i].val);
        var cA=sh.getRange(1,rA[i].col,10,1).getValues();
        for(var j=0;j<cA.length;j++) {
          Logger.log('col: %s, row: %s value: %s',rA[i].col, j+1, cA[0][j]);
        }
      }
    }
  }
// Create new sheet
function newSheet() {
 var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
 var yourNewSheet = activeSpreadsheet.getSheetByName("TourzettelV2");

    if (yourNewSheet != null) {
        activeSpreadsheet.deleteSheet(yourNewSheet);
    }

    yourNewSheet = activeSpreadsheet.insertSheet();
    yourNewSheet.setName("TourzettelV2");
}

// Log in das neue Sheet schreiben

function logOutput() {

var stringToWrite = Logger.log;
SpreadsheetApp.getActive().getSheetByName('TourzettelV2').getRange("A1").setValue(stringToWrite);

}

My problem now is the function logOutput, because Logger.log doesn't seem to work. It outputs:

function log() {
        [native code, arity=0]
}

Not sure what exactly it logged there, but it doesn't look like cell values. I do want every single logged value to be put into a seperate cell.




回答2:


To fix the NaN error try this:

if (isNan(rowIdx)) {
    Browser.msgBox("Numbers only or 'cancel'");
    return;
  }  

The other part is a little hard to follow. If you want to pass the row index number to the second function, you could try adding this line to the first function:

readData(rowIdx);

And the second function would then be

function readData(rowIdx) {
...
}

And it can then use the rowIdx variable in the script. I'm assuming the 2nd function does not need the spreadsheetID passed to it as it is provided in the first line of the function. (Or is that there because you were testing?)



来源:https://stackoverflow.com/questions/56990825/i-want-to-write-a-script-to-read-and-log-values-of-a-specific-row-and-copy-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!