找到&替换 - Google表格脚本

时间:2017-06-30 16:04:38

标签: javascript google-apps-script spreadsheet

我的脚本遇到了一些麻烦。基本上,这可以从一个课程获取数据并将值输入到一个工作表。这完美地运作了。 但是,当我的一个学生输入并输入'在那个课程的命令,我很难在Excel中阅读它。所以,我必须找到并替换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());
}

我可以在哪里输入:

     doc.replaceText("\r\n|\n|\r",";[[:space:]]"); 

在电子表格收到数据后直接工作?在课程完成后我无法打开工作表来手动完成或甚至播放脚本。 (抱歉任何语言错误)

非常感谢!!!!!!!!!!!!!!

1 个答案:

答案 0 :(得分:0)

基于此related post,您可以通过读取工作表中的所有值(作为数组),循环遍历数组,替换值,然后将整个数组写回工作表来实现此目的。 / p>

您可能需要查看此thread中的示例代码。

  

试试这个:

/usercallback
     

这次它将替换字符串中的find。我只能把它放回去   如果字符串被找到则替换,如果它更好。基本上,我更换:

function fandr() {
  var r=SpreadsheetApp.getActiveSheet().getDataRange();
  var rws=r.getNumRows();
  var cls=r.getNumColumns();
  var i,j,a,find,repl;
  find="abc";
  repl="xyz";
  for (i=1;i<=rws;i++) {
    for (j=1;j<=cls;j++) {
      a=r.getCell(i, j).getValue();
      if (r.getCell(i,j).getFormula()) {continue;}
      try {
        a=a.replace(find,repl);
        r.getCell(i, j).setValue(a);
      }
      catch (err) {continue;}
    }
  }
}
     

  try {
    a=a.replace(find,repl);
    r.getCell(i, j).setValue(a);
  }
  catch (err) {continue;}