找到&替换 - GetData之后的Google表格脚本

时间:2017-07-03 19:15:05

标签: google-apps-script scripting google-sheets

我的脚本遇到了一些麻烦。基本上,这可以从一个课程获取数据并将值输入到一个工作表。这完美地运作了。但是,当我的一个学生输入并输入'在那个课程的命令,我很难在Excel中阅读它。 所以,我必须找到(它是一个正则表达式:\ r \ n | \ n | \ r)并替换Google电子表格中的输入并将其更改为&#34 ;; &#34 ;.手动完美地工作,但我不能通过脚本来做。这件作品:

     //  1. Enter sheet name where data is to be written below
        var SHEET_NAME = "DATA";

//  2. Run > setup
//
//  3. Publish > Deploy as web app 
//    - enter Project Version name and click 'Save New Version' 
//    - set security level and enable service (most likely execute as 'me' and access 'anyone, even anonymously) 
//
//  4. Copy the 'Current web app URL' and post this in your form/script action 
//
//  5. Insert column names on your destination sheet matching the parameter names of the data you are passing in (exactly matching case)

var SCRIPT_PROP = PropertiesService.getScriptProperties(); // new property service

// If you don't want to expose either GET or POST methods you can comment out the appropriate function
function doGet(e){
  return handleResponse(e);
}
 function doPost(e){
  return handleResponse(e);
}

function handleResponse(e) {
  // shortly after my original solution Google announced the LockService[1]
  // this prevents concurrent access overwritting data
  // [1] http://googleappsdeveloper.blogspot.co.uk/2011/10/concurrency-and-google-apps-script.html
  // we want a public lock, one that locks for all invocations
  var lock = LockService.getPublicLock();
  lock.waitLock(30000);  // wait 30 seconds before conceding defeat.

  try {
    // next set where we write the data - you could write to multiple/alternate destinations
    var doc = SpreadsheetApp.openById(SCRIPT_PROP.getProperty("CHANGED BY SECURITY REASON"));
    var sheet = doc.getSheetByName(SHEET_NAME);

    // we'll assume header is in row 1 but you can override with header_row in GET/POST data
    var headRow = e.parameter.header_row || 1;
    var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
    var nextRow = sheet.getLastRow()+1; // get next row
    var row = []; 
    // loop through the header columns
    for (i in headers){
      if (headers[i] == "Timestamp"){ // special case if you include a 'Timestamp' column
        row.push(new Date());
      } else { // else use header name to get data
        row.push(e.parameter[headers[i]]);
      }
    }
    // more efficient to set values as [][] array than individually
    sheet.getRange(nextRow, 1, 1, row.length).setValues([row]);
    // return json success results
    return ContentService
          .createTextOutput(JSON.stringify({"result":"success", "row": nextRow}))
          .setMimeType(ContentService.MimeType.JSON);
  } catch(e){


    // if error return this
    return ContentService
          .createTextOutput(JSON.stringify({"result":"error", "error": e}))
          .setMimeType(ContentService.MimeType.JSON);
  } finally { //release lock
    lock.releaseLock();
  }
}


function setup() {
    var doc = SpreadsheetApp.getActiveSpreadsheet();
    SCRIPT_PROP.setProperty("1Un5A61M8CJDBGDAB-Tx-lYgKYaVB2RSfn9QAQ5Q-sZs", doc.getId());
}

我试过这个并且运作良好,但需要一个每分钟只播放一次的触发器。不行......并且只更改单元格中的第一个值。有时在同一个单元格中不止一个:

function Replace() {
    var redeletar = new RegExp("\r\n|\n|\r");
    var range = SpreadsheetApp.getActiveSheet()
        .getRange('A1:CH');
      range.setValues(range.getValues()
        .map(function (r) {
            return r.map(function (c) {
            //Replace string
              return c.toString().replace(redeletar, ";");
             });
     }));
}

在Sheet从我的课程接收数据并以递归方式播放之后,是否同时播放此脚本?

非常感谢

1 个答案:

答案 0 :(得分:0)

我认为您需要的是全局标志,如下所示:

function Replace() 
 {
   var re = /(\r\n|\n|\r")/g;
   var range = SpreadsheetApp.getActiveSheet().getActiveRange();
   range.setValues(range.getValues().map(function (r){return r.map(function (c){return c.toString().replace(re, ";");});}));
 }

我认为这将取代所有替代品,而不仅仅是第一批;