Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 在一次调用中设置一批父级/同级_Google Apps Script_Smartsheet Api_Smartsheet Api 1.1 - Fatal编程技术网

Google apps script 在一次调用中设置一批父级/同级

Google apps script 在一次调用中设置一批父级/同级,google-apps-script,smartsheet-api,smartsheet-api-1.1,Google Apps Script,Smartsheet Api,Smartsheet Api 1.1,我正在构建一个Google脚本,自动将Google电子表格写入Smartsheet,但是由于父/子表已经格式化,不需要手动索引,因为我已经在电子表格中通过编号列完成了这项工作 我设法写入了Smartsheet,但我似乎不知道如何发送一批(600多行)带有父/子行集的行,这些行位于同一个urlFetch调用中 一些应该解决我问题的问题: 是否可以确定/指定我将要包含的“rowId” 提供行时是否可以设置“parentRowNumber” 是否有其他方法可以在不知道“rowId”的情况下批量插入父行

我正在构建一个Google脚本,自动将Google电子表格写入Smartsheet,但是由于父/子表已经格式化,不需要手动索引,因为我已经在电子表格中通过编号列完成了这项工作

我设法写入了Smartsheet,但我似乎不知道如何发送一批(600多行)带有父/子行集的行,这些行位于同一个urlFetch调用中

一些应该解决我问题的问题:

是否可以确定/指定我将要包含的“rowId”

提供行时是否可以设置“parentRowNumber”

是否有其他方法可以在不知道“rowId”的情况下批量插入父行

由于我还没有在这里找到[google apps script]+[smartsheet api],下面介绍如何插入一行(工作代码):


您可以在Smartsheet API中插入多行,但该调用中的所有行都将添加到工作表中的同一heirachal级别。因此,要使用具有父/子关系的行创建图纸,可以执行以下操作:

首先,进行一次调用以将所有父级行插入到工作表中


然后,使用新创建的行的rowid,进行其他调用以发布每个父行的所有子行。您需要为每个父行的子行组单独呼叫。

原始问题的答案是否定的,但正如stmcallister所回答的,它既不漂亮也不快速,但有效

function preencherSmartSheet(){
  var v_sheetId = ##########; //Id planilha, definida no cronograma
  var dadosOrc = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Orçamento").getRange("A3:K").getValues();
  var rows = [], v_maxNivel = 0, v_linhasIds = [], calls = 0, payload, tempId;

  //Enviado ao smartsheet
  var dadosEnviar = accessToken; // Definido na aba Smartsheet API.gs
  dadosEnviar.Method = "post"; //post = incluir

  //Pega os dados da planilha Smartsheet
  var planilhaSmart = JSON.parse(UrlFetchApp.fetch("https://api.smartsheet.com/1.1/sheet/"+v_sheetId, accessToken));
  calls++;

  //Verifica qual a coluna das tarefas
  var idColuna = [];
  for(i in planilhaSmart.columns){
    if(planilhaSmart.columns[i].title == "Nome da tarefa"){
      idColuna[1] = planilhaSmart.columns[i].id;
    }else if(planilhaSmart.columns[i].title == "n°"){
      idColuna[0] = planilhaSmart.columns[i].id;
    }
  }

  //verifica maior nivel para começar
  for(i in dadosOrc){
    if(dadosOrc[i][10] > v_maxNivel){
      v_maxNivel = dadosOrc[i][10];
    }
  }

  //Formata as linhas em Objeto JSON
  for(v_nivelAtual = v_maxNivel; v_nivelAtual >= 0; v_nivelAtual--){
    for(i = 0; i < dadosOrc.length; i++){
      if(dadosOrc[i][10] == v_nivelAtual){
        if(v_nivelAtual == v_maxNivel){
          for(; i < dadosOrc.length; i++){
            if(dadosOrc[i][10] == v_nivelAtual){
              rows.push({"cells":[{"columnId" : idColuna[0].toString(), "strict" : false, "value" : i},
                                  {"columnId" : idColuna[1].toString(), "strict" : true, "value" : dadosOrc[i][0].toString()}]});
            }
          }
        }else{
          rows = [];
          tempId = i;
          for(; i < dadosOrc.length && dadosOrc[i][10] <= v_nivelAtual; i++){
            if(dadosOrc[i][10] == v_nivelAtual){
              rows.push({"cells":[{"columnId" : idColuna[0].toString(), "strict" : false, "value" : i},
                                  {"columnId" : idColuna[1].toString(), "strict" : true, "value" : dadosOrc[i][0].toString()}]});
            }
          }
        }

        if(v_nivelAtual < v_maxNivel){
          payload = {"toBottom":true, "rows":rows, "parentId" :  dadosOrc[tempId][1].toString()};
        }else{
          payload = {"toBottom":true, "rows":rows};
        }

        dadosEnviar.payload = JSON.stringify(payload);

        UrlFetchApp.fetch("https://api.smartsheet.com/1.1/sheet/"+v_sheetId+"/rows", dadosEnviar);
        calls++;
      }
    }


    planilhaSmart = JSON.parse(UrlFetchApp.fetch("https://api.smartsheet.com/1.1/sheet/"+v_sheetId, {headers:cabecalhoSmartsheet}));
    calls++;

    //Array com os id's da linhas pais, indice do array = linha do pai
    for(i in planilhaSmart.rows){
      v_linhasIds[planilhaSmart.rows[i].cells[0].value] = planilhaSmart.rows[i].id;
    }

    //Coloca no item 1 do array de dados do orçamento o id da linha pai das mesmas
    for(i in dadosOrc){
      if((dadosOrc[i][10]*1) == ((v_nivelAtual*1) - 1)){
        for(ii = i; ii >= 0; ii--){
          if(v_linhasIds[ii]){
            dadosOrc[i][1] = v_linhasIds[ii]
            ii = 0;
          }
        }
      }
    }
  }
  Logger.log(calls);
}
函数preencherSmartSheet(){
var v#u sheetId=#############//Id planilha,定义无cronograma
var dadosOrc=SpreadsheetApp.getActiveSpreadsheet().getSheetByName(“Orçamento”).getRange(“A3:K”).getValues();
var行=[],v_maxNivel=0,v_linhasIds=[],calls=0,payload,tempId;
//Enviado ao智能表
var dadosEnviar=accessToken;//Definido na aba Smartsheet API.gs
dadosEnviar.Method=“post”//post=incluir
//Pega os dados da planilha Smartsheet
var planilhaSmart=JSON.parse(UrlFetchApp.fetch(“https://api.smartsheet.com/1.1/sheet/“+v_sheetId,accessToken”);
调用++;
//验证是否符合条件
变量idColuna=[];
for(在planilhaSmart.columns中的i){
if(planilhaSmart.columns[i].title==“Nome da tarefa”){
idColuna[1]=planilhaSmart.columns[i].id;
}else if(planilhaSmart.columns[i].title==“n°”){
idColuna[0]=planilhaSmart.columns[i].id;
}
}
//核查工作
对于(我在dadosOrc中){
if(dadosOrc[i][10]>v_maxNivel){
v_maxNivel=dadosOrc[i][10];
}
}
//Formata as LINEM Objeto JSON
对于(v_nivelAtual=v_maxNivel;v_nivelAtual>=0;v_nivelAtual--){
对于(i=0;i对于(;ifunction preencherSmartSheet(){
  var v_sheetId = ##########; //Id planilha, definida no cronograma
  var dadosOrc = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Orçamento").getRange("A3:K").getValues();
  var rows = [], v_maxNivel = 0, v_linhasIds = [], calls = 0, payload, tempId;

  //Enviado ao smartsheet
  var dadosEnviar = accessToken; // Definido na aba Smartsheet API.gs
  dadosEnviar.Method = "post"; //post = incluir

  //Pega os dados da planilha Smartsheet
  var planilhaSmart = JSON.parse(UrlFetchApp.fetch("https://api.smartsheet.com/1.1/sheet/"+v_sheetId, accessToken));
  calls++;

  //Verifica qual a coluna das tarefas
  var idColuna = [];
  for(i in planilhaSmart.columns){
    if(planilhaSmart.columns[i].title == "Nome da tarefa"){
      idColuna[1] = planilhaSmart.columns[i].id;
    }else if(planilhaSmart.columns[i].title == "n°"){
      idColuna[0] = planilhaSmart.columns[i].id;
    }
  }

  //verifica maior nivel para começar
  for(i in dadosOrc){
    if(dadosOrc[i][10] > v_maxNivel){
      v_maxNivel = dadosOrc[i][10];
    }
  }

  //Formata as linhas em Objeto JSON
  for(v_nivelAtual = v_maxNivel; v_nivelAtual >= 0; v_nivelAtual--){
    for(i = 0; i < dadosOrc.length; i++){
      if(dadosOrc[i][10] == v_nivelAtual){
        if(v_nivelAtual == v_maxNivel){
          for(; i < dadosOrc.length; i++){
            if(dadosOrc[i][10] == v_nivelAtual){
              rows.push({"cells":[{"columnId" : idColuna[0].toString(), "strict" : false, "value" : i},
                                  {"columnId" : idColuna[1].toString(), "strict" : true, "value" : dadosOrc[i][0].toString()}]});
            }
          }
        }else{
          rows = [];
          tempId = i;
          for(; i < dadosOrc.length && dadosOrc[i][10] <= v_nivelAtual; i++){
            if(dadosOrc[i][10] == v_nivelAtual){
              rows.push({"cells":[{"columnId" : idColuna[0].toString(), "strict" : false, "value" : i},
                                  {"columnId" : idColuna[1].toString(), "strict" : true, "value" : dadosOrc[i][0].toString()}]});
            }
          }
        }

        if(v_nivelAtual < v_maxNivel){
          payload = {"toBottom":true, "rows":rows, "parentId" :  dadosOrc[tempId][1].toString()};
        }else{
          payload = {"toBottom":true, "rows":rows};
        }

        dadosEnviar.payload = JSON.stringify(payload);

        UrlFetchApp.fetch("https://api.smartsheet.com/1.1/sheet/"+v_sheetId+"/rows", dadosEnviar);
        calls++;
      }
    }


    planilhaSmart = JSON.parse(UrlFetchApp.fetch("https://api.smartsheet.com/1.1/sheet/"+v_sheetId, {headers:cabecalhoSmartsheet}));
    calls++;

    //Array com os id's da linhas pais, indice do array = linha do pai
    for(i in planilhaSmart.rows){
      v_linhasIds[planilhaSmart.rows[i].cells[0].value] = planilhaSmart.rows[i].id;
    }

    //Coloca no item 1 do array de dados do orçamento o id da linha pai das mesmas
    for(i in dadosOrc){
      if((dadosOrc[i][10]*1) == ((v_nivelAtual*1) - 1)){
        for(ii = i; ii >= 0; ii--){
          if(v_linhasIds[ii]){
            dadosOrc[i][1] = v_linhasIds[ii]
            ii = 0;
          }
        }
      }
    }
  }
  Logger.log(calls);
}